Problem with multiple output statements in DO WHILE loop.
I'm having some trouble figuring out why this code is outputting twice. When I run this method in my main program it will output the phrase "That is not the name of a sphere" no matter what the value is. The loop structure works, i.e. if the name isn't defined in my array then it will keep asking me until I write the name of a sphere that is and then will progress with the method, but even if the sphere is correctly named, it will print that phrase before continuing with the method. I know that the else statement is printing no matter what but I cannot find where the error is. This error only occurs when the array is greater than one element. If I only enter one sphere into the array it will only ask once per test. Any help would be greatly appreciated. Here's the code:
****nevermind. I fixed it. I just used a counting variable when the argument was true instead of setting a boolean. It was going through each array member and printing the phrase for each one that didn't match the string. Oh well, this is why we take classes.
Code :
do{
System.out.println("Enter the name of the sphere that you would like to transpose: ");
temp=input.nextLine();
for(int i=0;i<x.length;i++){
if(temp.equals(x[i].getName())){
test=true;
}
else
System.out.println("That is not the name of a sphere.");
}
}while(!test);