Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Problem with multiple output statements in DO WHILE loop.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default 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.



    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);
    Last edited by Nismoz3255; February 22nd, 2012 at 10:12 PM.


Similar Threads

  1. Creating multiple objects with a for loop
    By JackCannon15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2011, 06:26 PM
  2. Problem with if statements
    By Omnamah in forum Loops & Control Statements
    Replies: 4
    Last Post: September 4th, 2011, 11:13 AM
  3. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 09:20 AM
  4. Output Largest and Smallest Integers using only If statements
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2011, 04:55 PM
  5. Creating multiple arraylists with a loop?
    By DLX in forum Collections and Generics
    Replies: 4
    Last Post: December 28th, 2010, 05:58 PM