Reputation: 13
I have a slight problem with my program and I'm sure it lies in this loop and if-else statement:
for (int i = 0; i < xArray.length; i++)
{
if(searchNumber == xArray[i])
{
found = true;
if(found)
{
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );
}
else
{
System.out.println("No match was found.");
}
break;
}
}
What happens when I execute the program is that if the searchNumber (which is taken via the Scanner with keyboard input) is found then it will display the appropiate message saying it was found, but if it isn't found no message is displayed. I tried debugging it by talking myself through what every line of code is doing, but when I got to this loop I got very confused.
Upvotes: 0
Views: 144
Reputation: 1746
Let's analyze the meaning of your code: First, there is a loop scanning through the array. Nothing's wrong with that. In the loop there is an if-statement
with the condition searchNumber == xArray[i]
this condition checks if there is a match. If there is one, it sets found
to true. Then an if-statement checks for found
and as found
has been set true
in the previous line, the code in the statement will always be executed, but the code in the else
will never be executed. So that's where the problem is.
So the code to find a number should be like this:
for(int i : xArray){ //Foreach loop to simplify
if(i == searchNumber){//True if i == number we're searching for
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );//Print the message
found = true; //The number has been found
break; //cancel the loop as the number is found.
}
}
//The array has been searched completely
if(!found){//if the number has not been found
System.out.println("No match was found.");//Print the message
}
Upvotes: 0
Reputation: 14286
Make sure found
starts as false
. Then do the printing after the loop is complete (or broken out of).
boolean found = false;
int i;//thanks @DaveNewton !
for (i = 0; i < xArray.length; i++)
{
if(searchNumber == xArray[i])
{
found = true;
break;
}
}
if(found)
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );
else
System.out.println("No match was found.");
Upvotes: 2
Reputation: 7082
Dusting off the old Java hat. This seems more concise?..
int position = xArray.indexOf(searchNumber);
if (position > -1) {
System.out.println("We have found your number " + searchNumber + " at position " + position + " in the array. " );
} else {
System.out.println("No match was found.");
}
Just thought - if this is not already an ArrayList, then use:
java.util.Arrays.asList(xArray).indexOf(searchNumber)
Upvotes: 1
Reputation: 2476
You set the variable found
to true, then have a test if (found)
, which will always be true as well. The else
clause will never execute, because found
will never be false.
Based on your description it sounds like you need to initialize found
to false outside the loop. Then if you get a match, set it to true. The if/else statement can then be moved outside the for loop and will behave as you expect it to.
Upvotes: 0
Reputation: 8767
For starters you're prematurely optimizing. Unless your array is obscnely large stopping the check with a break is not necessary. Its good to be efficient, but not at the expense of not working.
When you want to end a loop early I like to use this approach with a while loop instead of a for loop:
//init our sentinel flag and our counter
boolean found = false;
int i = 0 ;
//while we have not found anything and we are less than the size of the array
while (!found && i < xArray.length)
{
//check our condition
found = (searchNumber == xArray[i]);
i++;
}
//finally react to the terminal state, found or not found and output as needed
if(found)
{
System.out.println("We have found your number " + searchNumber + " at position " + i + " in the array. " );
}
else
{
System.out.println("No match was found.");
}
This is functionally equivalent to a for loop with a break as seen in @CyberneticTwerkGuruOrcs answer. Some developers find it easier to read. This also discourages the use of break which can sometimes be hard to determine which statement is being broken out of.
Upvotes: 0