Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Scanner: InputMismatch exception

  1. #1
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Scanner: InputMismatch exception

    Could anyone give me hint. Atm when input != integer, it prints mismatch error and outputs the ArrayList. How can I make so when the input != integer, it prints the error but returns back to the user to get a new input.
    Java Code:
       public void inputToFile(String name) {
          PrintWriter output;
          al = new ArrayList<Integer>();
          int number;
          
          //Sisesta numbrid
          while(true) {
             try {
                number = input.nextInt();
             } catch (InputMismatchException e) {
                System.out.println("Only integers");
                break;
             }
             if (number != 0)
                al.add(number);
             else
                break;
             
          }
          System.out.println( al.toString());
          //Part2
          try {
             output = new PrintWriter(new FileWriter(name));
             for (int i = 0;i<al.size();i++) {
                output.println(al.get(i));
             }
             output.close();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Scanner: InputMismatch exception

    How can I make so when the input != integer, it prints the error
    You can wrap the nextInt method in a try{}catch block that catches the InputMismatch exception.
    In the catch block you can print an error message and then return back to allow the user to enter good data.

    Your code already has that. It also has a break statement that throws the execution out of the loop.
    A continue statement instead would return the execution to the beginning of the loop.

  3. #3
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Scanner: InputMismatch exception

    A continue statement instead would return the execution to the beginning of the loop.
    I tried before replacing it with continue; Also I added
    Java Code:
    System.out.println("Enter number");
    in front of
    Java Code:
    number = input.nextInt();
    because then I can clearly explain what had happened. After I had replaced break with continue I encountered with next problem. Program looped between
    Java Code:
    Enter number
    Only integers
    Enter number
    Only integers
    and never asked again. It returned to beginning of loop, but never asked new integer from user.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Scanner: InputMismatch exception

    If the input doesn''t contain an int (no digit to start with), the Scanner doesn't read anything and the offending character stays in the input buffer; if an error occurs better skip the entire line and get rid of the offending character.

    kind regards,

    Jos
    Norm likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Scanner: InputMismatch exception

    Thank you, problem solved ;)

Similar Threads

  1. Replies: 1
    Last Post: 06-05-2011, 08:32 PM
  2. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  3. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  4. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •