Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

my linear search logic went wrong why?

 
obaid abbassi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



int search[]={40 ,13, 19 ,27, 11, 43 ,48 ,55 ,59, 22, 75, 87, 94};


String q=JOptionPane.showInputDialog("Enter your number to search","");
int b= Integer.valueOf(q) ;

for(int i=0;i<search.length;i++){

   

  int p=search[i] ;

if(p==b){

    System.out.println("nmuber found at "+i );
 
  return;
}
else{
    System.out.println("not found");
 
   
    return;
}

}
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Next time, please use codetags!

You have this code: (a little neater formatted)

Suppose you search for 40. Then p == b and so the index is reported. However, if you search for 13, then first 40 is being checked, and p != b. What happens then, and what should you do to fix this?
 
obaid abbassi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i remove else part my search works but with else I always get not found?
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your input is 13, then first 13 is compared to 40. Since the two are unequal, your else part will be executed, resulting in "not found". So, you must bring the "not found" message outside of the loop.
 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

obaid abbassi wrote:if i remove else part my search works . . .

No, I don't think your search will work at all. You  need to remove more than the else.
A linear search does not usually print the index; it returns the index, or −1 to signal value not found.
 
obaid abbassi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you change a code for me?
 
Carey Brown
Saloon Keeper
Posts: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can help but first we ask that you put in some effort. Reformat your code with proper indentation and eliminate all the blank lines. Next remove the 'else' part, or better yet, move it after the loop is done. Then repost the code here and UseCodeTags (<-read this link).
 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also find it helpful to write down how you think the logic would work. Don't use your computer (silicon) but use carbon‑based data recording (carbon as in pencil, also paper and eraser). You will end up with something like thisThe first time round, “next element” will take you to the first element. and lines 2 and 4‑5 taken together represent a loop. Line 3 is what you are going to have to work out.
 
obaid abbassi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args) {
       // TODO code application logic here
 
int search[]={40 ,13, 19 ,27, 11, 43 ,48 ,55 ,59, 22, 75, 87, 94};


String q=JOptionPane.showInputDialog("Enter your number to search","");
int b= Integer.valueOf(q) ;

for(int i=0;i<search.length;i++){

   

  int p=search[i] ;

if(p==b){

    System.out.println("nmuber found at "+i );
 
  return;
}
else{
    System.out.println("not found");
 
   
    return;
}

}

}
 
Liutauras Vilda
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been pointed out earlier. Could you please add code tags when posting the code.
 
Campbell Ritchie
Marshal
Posts: 74084
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have posted illegible code which looks hardly any different from the previous offering. That suggests you haven't tried to work out what the correct way to do a linear search is. As I said last week, you need to write the logic on paper (or a WP document) before you try writing any code.
We won't give you a complete solution because you will learn much more if you work out the problem yourself.
 
obaid abbassi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in this case i can search but else statement is also executing although I m using break statement why?


int size=0;
System.out.println("please initialize your array first");
Scanner sc=new Scanner(System.in);
size =sc.nextInt();
int search[]=new int[size];
       System.out.println("The size of array is "+search.length);
       System.out.println("PLEASE INPUT VALUES IN ARRAY");  
for(int i=0;i<search.length;i++){

search[i]=sc.nextInt();

                                 }
System.out.println("The values are:");

for(int i=0;i<search.length;i++){
System.out.println(""+search[i]);
                                 }
  System.out.println("please find the number in given array");
       
      int q= sc.nextInt();
     
      for(int i=0;i<search.length;i++){

            if(q==search[i]){


   System.out.println("NUMBER FOUND AT "+i);
return;

                              }
else{System.out.println("number not found");}


}
 }
 
Carey Brown
Saloon Keeper
Posts: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:We can help but first we ask that you put in some effort. Reformat your code with proper indentation and eliminate all the blank lines. Next remove the 'else' part, or better yet, move it after the loop is done. Then repost the code here and UseCodeTags (<-read this link).

UseCodeTags (<-link)
 
reply
    Bookmark Topic Watch Topic
  • New Topic