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

Thread: User Input Loop

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default User Input Loop

    I've been messing around with this for a bit and cannot seem to code it so that it will work as intended. I'm trying to prompt the user for input repeatedly, until the user enters a -1, when the loop is supposed to terminate.

    My problem lies in that the loop does not stop when -1 is entered. What am I doing wrong here?

    import java.util.Scanner;
     
    class Test{
     
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String in = scan.nextLine();
     
        while(in != "-1"){
          System.out.println("You entered: " + in);
          in = scan.nextLine();
     
        if(in == "-1")
          System.out.println("Stopped");
          break;
       }
      }
     }
    Last edited by cfmonster; August 17th, 2009 at 12:30 PM.


  2. #2
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Input Loop

    BTW,

    do in.trim().equals("-1") instead of in == "-1"

    also print and see what it is printing?

    Please note that you are doing redundant check in your while just for entry check.

    probably, you could write:

    if (!in.trim().equals("-1")) {
    while (1) {
    ....// rest code
    if(in.trim().equals("-1"))
    System.out.println("Stopped");
    break;
    }
    }
    }
    Acumen,
    Lucid forums

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: User Input Loop

    Here's another one of my best practise tips, whenever you are doing string comparison on constant strings, always call the equals method on the constant to avoid null pointers.

    if ("-1".equals(input)) {
        // Do some magic!
    }

    // Json

  4. #4
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Input Loop

    Agreed, I would suggest the same for all cases like

    int i = fun();

    if (-1 == i) {
    // do something
    } else {
    // do something else
    }
    In such case u don't end up doing

    -1 = i, but i = -1 would have messed up things for you

    Acumen,
    Lucid forums

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: User Input Loop

    Thanks. I hadn't realized that there was a built in method for string comparison.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: User Input Loop

    while(1) won't work... Java has abstracted that away

    Yes, Java's comparison is different because of the way it creates objects for pretty much everything. Using the == operator only compares the addresses the objects are held at, instead of their actual content.

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: User Input Loop

    so the "-1" will be serve as a character?

  8. #8
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: User Input Loop

    Quote Originally Posted by chronoz13 View Post
    so the "-1" will be serve as a character?
    The "-1" in this example is being read as a String.

    Double quotes are for string representation, while single quotes are for character representation.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Libraries for User Tracking
    By jwpa in forum The Cafe
    Replies: 4
    Last Post: August 4th, 2009, 09:32 AM
  4. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM