Results 1 to 2 of 2
  1. #1
    lowziqian is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Whats wrong with my while loop?

    the task is to let the user input item code, and quantity he wants until he inputs "XX" to terminate the looping.
    but my code seems to stop at one loop.
    Q: do i have to use an array? or is it a logic problem? ( i will have to use the string inputs in the later part)
    ps: i do not want an answer, i just want to know what misconception i have.

    System.out.println("ENTER CODE (XX to Stop):");
    System.out.print("CODE: ");
    String i=scan.nextLine();



    System.out.println("ENTER QUANTITY: ");
    int y=scan.nextInt();
    System.out.println("ENTER CODE :");

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Whats wrong with my while loop?

    There's nothing wrong with your while loop; it's the nextInt() method that's acting up; that method only reads digits but if you type in a number, say 42, you type '4' '2' and <enter>. The nextInt() method leaves the <enter> character(s) in the input buffer so the following nextLine() method assumes that it just read an empty line. The idiom is to add a dummy readLine() call just after you have read a number to get rid of that <enter> character in the input buffer.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Whats wrong with my code
    By zit1343 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 01:06 AM
  2. Whats wrong with my program?
    By jwb4291 in forum New To Java
    Replies: 7
    Last Post: 12-12-2010, 04:40 AM
  3. whats wrong
    By atenv in forum New To Java
    Replies: 6
    Last Post: 06-15-2010, 01:55 PM
  4. whats is wrong with this app??
    By mrajan in forum New To Java
    Replies: 4
    Last Post: 06-09-2010, 10:56 PM
  5. Database help... whats wrong?
    By neosnokia in forum JDBC
    Replies: 4
    Last Post: 06-09-2009, 11:17 PM

Posting Permissions

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