• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do While Loop Does Not Loop  RSS feed

 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a loop that requests the user to enter a name to search an array. The only reason for the loop to stop is if the user enters "quit" or "exit".
Below is the code I have, I am trying a do while loop, but it always stops the search after one attempt. Additionally, it prints line 43 or 48 twice (depending on the result) when it should only print once.



Thank you for your time.
 
Paweł Baczyński
Bartender
Posts: 2082
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write while(condition == true) or while (condition == false).

This is very error prone. You could easily write an assignment instead. Well... You did.

Always use while(condition) or while(!condition).
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Never write while(condition == true) or while (condition == false).

This is very error prone. You could easily write an assignment instead. Well... You did.

Always use while(condition) or while(!condition).


I changed my while condition to !=true and that made the loop work. But it prints line 43 or 48 twice (depending on the result) when it should only print once.

Here is my output when I search for buzz:

Enter a name to search for(type exit or quit to stop the program):
buzz
Searching for buzz...

buzz is found in the array.
buzz is found in the array.
Number of comparisons to find buzz: 61
Enter a name to search for(type exit or quit to stop the program):


It looks good except for "buzz is found in the array." printing twice. I don't see anything in my code that would make it print twice, but its there.

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use != true. It may work now, but its still bad style, and its redundant.

Use !stopSearch instead.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, using !stopSearch, that is running my loop fine. Still getting the double print out though.

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:
I changed my while condition to !=true and that made the loop work. But it prints line 43 or 48 twice (depending on the result) when it should only print once.

Here is my output when I search for buzz:

Enter a name to search for(type exit or quit to stop the program):
buzz
Searching for buzz...

buzz is found in the array.
buzz is found in the array.
Number of comparisons to find buzz: 61
Enter a name to search for(type exit or quit to stop the program):


It looks good except for "buzz is found in the array." printing twice. I don't see anything in my code that would make it print twice, but its there.



I don't get that output when running your code (after modifying the while loop to have stopSerach != true as you say you have. I get the following output:

Enter a name to search for(type exit or quit to stop the program):
buzz
Searching for buzz...
buzz is found in the array.
Enter a name to search for(type exit or quit to stop the program):
exit
Searching for exit...
exit not found in the array
exit not found in the array
Number of comparisons to find exit: 91
Average of 45 comparisons per linear search


Now your code contains two calls to linearSearch(...) which is why you're getting extra lines printed saying what was searched for. It also searches for the word exit and quit when you type them, which I'm assuming was not meant to happen.

Edit: I typed this post before your post immediately above this was there Scott.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:



Take a look at the lines in bold. If you call the linearSearch method twice it will run twice...

Edit: of course the bold doesn't work in the code block
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that overwriting the value of a parameter (as you do with the parameter i in the linearSearch() method) is considered bad style. All parameters should be final otherwise it gets confusing, especially when people don't realise Java uses pass-by-value semantics and they think overwriting the parameter affects the calling site too.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
Take a look at the lines in bold. If you call the linearSearch method twice it will run twice...


That took care of the double printing issue. I didn't realize that "i = SearchSort.linearSearch(0, nameToSearch);" would call the linearSearch method.
Thank you.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:
That took care of the double printing issue. I didn't realize that "i = SearchSort.linearSearch(0, nameToSearch);" would call the linearSearch method, I thought it would assign the value of i in linearSearch to the value of i in the main method.


The i variable in the linearSearch() is a local variable. It is only in scope when the method is running. So, assigning it without calling it doesn't make sense.

Scott M Summers wrote:
Is there a way to pass the value of i from the linearSearch method to my main method?


The i variable is the first parameter, so the answer is "yes", pass it as the first parameter -- instead of passing zero... Not that it matters, as the first thing the method does is set the i variable to zero.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!