Results 1 to 6 of 6

Thread: Error in method Scanner.nextInt()

  1. #1
    Join Date
    Mar 2010
    Posts
    338

    Error in method Scanner.nextInt()

    Hello,
    I have the following problem, the method nextInt () of class acts java.util.Scanner not the way I expect this, Short cut in the program my problem is summarized.
    Code:
    import java.util.* ;
    class tst (public static void main (String [] args) 
    (scanner snc = new scanner (System.in); 
    while (true) (int x = 0; System.out.println ("Choose 1,2,3 "); 
    snc.nextLine String str = ();
     try (x = Integer.parseInt (str);) 
     catch (Exception e) (System.out.println (str +" Failure ");) 
     if ( x == 1) (System.out.println ("nerf");) if (x == 2) (System.out.println ("Please enter age"); 
     int j = snc.nextInt (); 
     system. out.println ("2");) if (x == 3) (System.out.println ("Exit") System.exit (0);))))
    I am getting any error in this code, but do no know why. Please help me with this. Thank you.

  2. #2
    Join Date
    Sep 2009
    Posts
    143

    Re: Error in method Scanner.nextInt()

    I do have a same problem, I know the one that nextInt the error I describe the cause but why? I now try again to describe my problem knee. I enter as the first one, then I have no problem Now when I enter 2, I am asked to input by the age of age he now jumps back to the beginning of grinding and was there again waiting for the selection, but that he does not, but immediately raises an error, Why? Exactly this behavior in the code, I do not understand because this is not really going to happen. When I type a number, the program I get an error. Please can anyone come up with this problem. Thank you.

  3. #3
    Join Date
    Dec 2009
    Posts
    213

    Re: Error in method Scanner.nextInt()

    Do you use Linux / UNIX? Then lies on the console. The review normally have to wait for the electricity that comes when LF. What helps, however you can find out here. Hopefully I've flown over the problem correctly.

  4. #4
    Join Date
    Sep 2009
    Posts
    143

    Re: Error in method Scanner.nextInt()

    No, I am using Windows XP, and whole is actually not much of a real problem, as I have already found one anyway Work around against the infinite loop, but I'm surprised this behavior because I have no explanation. But maybe I can make someone understand my exact problem.

  5. #5
    Join Date
    Dec 2009
    Posts
    292

    Re: Error in method Scanner.nextInt()

    o get around the whole we just have the catch block of nextInt () method of the command, the method sc.nextLine () call this then reads from the incorrect input and thus empties the buffer. and the next iteration does nextInt () method then returns to normal.

    Code:
    import java.util; .* 
    class test (public static void main (String [] args) 
    (int a = 0; Scanner scnr = new Scanner (System.in); 
    bl boolean = false; do (bl = false; 
    system. out.println ("Enter your old one"); try (a = scnr.nextInt () / / <- here, if the incorrect input is made (eg a letter)) catch (InputMismatchException e) 
    (System.out. (e + "Please enter Numeric println"); 
    scnr.nextLine () / / <- this line would be without a broken input at both nextInt / / method an infinite loop resulting bl = true;)) while (bl);))

  6. #6
    Join Date
    Mar 2010
    Posts
    338

    Re: Error in method Scanner.nextInt()

    I also read true labs, have to say but variable, that I find it very difficult this behave much meaning. Yet if you do not read from System.in but otherwise?

Similar Threads

  1. Getting error: HTTP Error 405 Method not allowed
    By Mongoli in forum Technology & Internet
    Replies: 4
    Last Post: 20-04-2012, 07:24 AM
  2. Method overriding versus method hiding in C#
    By ^MALARVIZHI^ in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 06:25 AM
  3. Replies: 5
    Last Post: 15-12-2010, 07:18 PM
  4. HP CUE Scanner error
    By Jolie-Pitt in forum Windows XP Support
    Replies: 2
    Last Post: 29-07-2009, 03:10 AM
  5. error CS1501: No overload for method
    By ALEXANDRO in forum Software Development
    Replies: 3
    Last Post: 13-07-2009, 09:51 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,439,873.58675 seconds with 16 queries