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 5 of 5

Thread: Loop Help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop Help

    The logic of my program works. I am having a hard time asking the user "do you want to do another? yes or no." and user would type yes or no, for the scanner class to read as a string. If the user types yes the program will start over if the user says no the program will say " Thanks for playing." I got it to print " Do you want to do another? yes or no" and used a scanner class. However the scanner class is not working. Can anyone help me... THanks in advance.

     import java.util.Scanner;
     public class test4 
    { 
    	public static void main (String[] args)
    	{
    		Scanner scan = new Scanner (System.in);
     
    		int userNum, total, addNum = 0, totalNum;
    		String play = "yes";
     
    		while ( play == "yes" && play != "no") 
    		{
    			System.out.println("Enter a number that is 3 or higher.");
    			userNum = scan.nextInt(); 
     
    			if (userNum >= 3)
    			{
    				for(int count = 3; count<userNum; count +=2)
    				{
    					addNum = addNum + count;
    				}
    				System.out.println("The sum of the ODD integers from 3 to " + userNum +  " is " + addNum);
    				System.out.println(" ");
    				System.out.println ("Do you want to do another? yes or no");
    				play = scan.nextLine( );
    			}
     
     
    			if(userNum < 3)
    			{
    				System.out.println("Error: The value must be greater than two");
    				System.out.println(" ");
    			}
    		}
    		while (play == "no")
    		{
    			System.out.println("Thanks Good Bye.");
    		}	
    	}
    }
    Last edited by Robsterr; October 19th, 2011 at 10:34 AM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Loop Help

    When doing a string compare use the function: String.equals(String)
    The reason is a String is an object and "==" is a binary compare. Two different strings which have the same content, are not actually identical.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop Help

    while (play == "no") 
            { 
                System.out.println("Thanks Good Bye."); 
            }

    You know the impact of this code in your program?
    This will run an infinite loop saying your specified print statement.
    Better use if-else. That was just a suggestion.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Help

    i am having a hard time with this as well. would putting a 'do' statement in there make it easier? what would it look like? and where would the loop go exactly?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop Help

    Quote Originally Posted by mbae View Post
    i am having a hard time with this as well. would putting a 'do' statement in there make it easier? what would it look like? and where would the loop go exactly?
    You put that loop there? Tell us, why do you need to use loop???
    You just want him to say good bye, why don't you just try it once??? I mean why loop?

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  2. help using for while loop
    By robertsbd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 30th, 2010, 02:36 PM
  3. help with a for loop
    By newbie79 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 02:20 AM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM