Results 1 to 5 of 5
  1. #1
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default help with loop. yes no

    [RESOLVED!] :) thank you!!


    Hey! I'm new to the forum but i recently started learning Java. If anyone could point me in the right direction here i would appreciate it!


    Java Code:
    // asks user to enter yes, if user does not input yes program loops 
    // wile loop style
    
    import java.util.Scanner;
    
    public class Test1
    
    {
    	public static void main(String[] args)
    	
    	{
    		String repeat;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("Enter Yes to continue");
    			repeat = keyboard.nextLine();
    		
    		while (repeat != 'Yes')
    		{
    			System.out.println("Enter Yes continue");
    			repeat = keyboard.nextLine();
    		}
    		
    	}
    }

    what am i doing wrong? :confused:
    Last edited by glina126; 02-17-2011 at 07:50 AM. Reason: resolved

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Don't use != to compare Strings (or any other objects) use equals() or !equals().

    equals() is a method declared for class Object but other classes define their own version to mean what makes sense for them. In the case of String there is an equals() that means "is made up of the same characters in the same order".

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Oh and you can't use single quotes for String literals.

    So,

    Java Code:
    while (!repeat.equals("Yes"))
        {
            // etc

  4. #4
    glina126 is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    thanks so much!!!!!!!!!! i like the quick help and great responses! :D thanks!!!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    (and welcome to the forum!) Just a tip: if you compile (and you should compile often...) and get a message you can't understand, post the entire compiler message. This would have alerted me to the single quotes. Likewise, *describe* the problem ie say what the undesired program behaviour is. ("doesn't stop when I enter Yes").

    Programmers are very literal minded and like to be given the facts. (even the obvious ones). But there is another reason for being precise and specific: I find with the mistakes that I make that I am often closer to figuring out an answer when I have asked *myself* a precise and specific question. Being able to frame such questions (and doing so is neither easy nor natural) is a good problem solving technique.

    (this rant/musing is not directed at you specifically - it just occured to me when I reread your original post and I thought would express it for the benefit - or otherwise - of all ;)

    Again, welcome.
    Last edited by pbrockway2; 02-17-2011 at 10:26 PM.

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •