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

Thread: Please held. What is wrong with my code? I am a beginner ;)

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

    Default Please held. What is wrong with my code? I am a beginner ;)

    In this program the user is buying some online stuff. A menu is displayed with the available stuff. The user is asked to enter the code of the object he needs. Until the user enter "XX" the program keeps looping.
            System.out.print("ENTER CODE (XX to Stop): ");
            System.out.println();
            System.out.print("CODE: ");
            String code = in.nextLine(); 
            System.out.print("QUANTITY: ");
            int quantity = in.nextInt(); 
     
            while (code.compareTo("XX")!=0 )
                {
                System.out.print("CODE: ");
                code = in.nextLine(); 
                System.out.print("QUANTITY: ");
                quantity = in.nextInt(); 
                }
    Can someone please compile the program and tell me what is wrong with my program? When the user enter the second object the following error is coming up, If the user enters "XX" the program compiles correctly:
     

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at ass2012.Ass2012.main(Ass2012.java:51)
    Java Result: 1



    Looking forward to hear from you! THANKS
    Last edited by helloworld922; December 30th, 2011 at 05:10 PM.


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

    Default Re: Please held. What is wrong with my code? I am a beginner ;)

    The issue is that nextInt() doesn't consume the end of line characters after you enter in a number and press enter. The solution is to add in a dummy nextLine() call that will consume this character.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please held. What is wrong with my code? I am a beginner ;)

    oucch that's a tricky one! thanks for your feedback!

Similar Threads

  1. Beginner pleasee help with java code
    By chuy525 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 06:38 PM
  2. Replies: 3
    Last Post: October 19th, 2011, 11:55 PM
  3. ServletExample code problem - beginner
    By Sawubona in forum Java Servlet
    Replies: 5
    Last Post: July 4th, 2011, 05:57 AM
  4. Very beginner - What's wrong in my applet declaration?
    By rforte in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2010, 04:54 AM
  5. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM

Tags for this Thread