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

Thread: Getting Exception " java.util.InputMismatchException" in scanner package

  1. #1
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Getting Exception " java.util.InputMismatchException" in scanner package

    hey, thanks for helping me out before i've developed some knowledge of the scanner class now, though i have run into another problem, i have made an example program to show my error.

    package testpackage;
    import java.io.*;
    import java.util.*;
     
     
    public class Main {
     
     
        public static void main(String[] args) {
    System.out.println("input");
    //for (int Loop = 0;Loop < 1;){
    Scanner sc = new Scanner(System.in);
    int name = sc.nextInt();
    if(name > 0 && name < 41){
        System.out.println("Working");
        //Loop = 1;
    }else{
        System.out.println("error");
    }
    //System.out.println("Hello");
    }
     
        //}
     
    }
    my program works fine providing that the user will enter a numerical value, but if i add any value that is not numerical i get the error
    Exception in thread "main" java.util.InputMismatchException
    i am unsure how to fix this error, i don't need to parse the character values though i will need it to continue working if the character value is entered.

    thank you in advanced,
    luke.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: checking if it is a character?

    Hello luke,

    The reason you are getting this error is because you are using int name = sc.nextInt();

    For a start, the variable 'name' is an Integer so needs to have an Integer value assigned.

    It's the Scanner class that is throwing the java.util.InputMismatchException exception.

    sc.nextInt(); is looking for an Integer value entered into the console.
    The Scanner class has several methods such as nextBoolean(), nextDouble(), nextFloat() which all look for the corresponding variable type.

    If you want to use characters or String, then you can use sc.next()

    Take a look at this example:

    import java.util.Scanner;
     
    public class Main2 {
     
        public static void main(String[] args) {
     
            System.out.println("input");
     
            Scanner sc = new Scanner(System.in);
     
            String name = sc.next();
     
            System.out.println(name);
     
        }
     
    }
    We can catch this java.util.InputMismatchException exception in a try/catch block. This is error handling and will alert the user that they must use an Integer value.

    import java.util.Scanner;
     
    public class Main2 {
     
        public static void main(String[] args) {
     
            try{
     
            System.out.println("input");
     
            Scanner sc = new Scanner(System.in);
     
            int number = sc.nextInt();
     
            System.out.println(number);
     
            }catch(Exception e){
                System.out.println("You must enter a Integer value!!");
            }
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: checking if it is a character?

    is there a way to loop this back so that it prompts the user again to enter the value?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: checking if it is a character?

    Quote Originally Posted by luke View Post
    is there a way to loop this back so that it prompts the user again to enter the value?
    Yes there is. You can use a while loop:

    import java.util.Scanner;
     
    public class Main2 {
     
        public static void main(String[] args) {
     
            System.out.println("Enter something!");
            Scanner sc = new Scanner(System.in);
     
            while(sc.hasNext()){
     
            String name = sc.next();
     
            System.out.println(name);
     
            }
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: checking if it is a character?

    hey i think i need to explain myself a bit more.
    i want to store a user input that can be used to compare a user input to another int,Thus the format must be in INT(i think...) form.

    and i wanted it so that, if you recieved that error message(because the value entered was not of numerical value) then you would be told that there was an error in your input and it would loop and prompt you for an input once again.

    cheers,
    luke.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: checking if it is a character?

    Take a look at this luke.

    If the user enters a non Integer value, the exception is caught and the ScannerStuff() method is run again.

    The 2 Integers myInt & yourInt are compared once a user enters an Integer value. If they match then a message is printed to the console.

    I hope this is what you are looking for...

    import java.util.Scanner;
     
    public class Main2 {
     
        public static void ScannerStuff() {
     
            int myInt = 10;
            int yourInt;
     
            System.out.println("Enter an Integer: ");
            Scanner sc = new Scanner(System.in);
     
            try {
                while (sc.hasNext()) {
     
                    yourInt = sc.nextInt();
                    System.out.println(yourInt);
     
                    if(yourInt == myInt){
                        System.out.println("You entered the same Integer as me!");
                    }
     
                }
            } catch (Exception e) {
                System.out.println("You must enter an Integer!");
                ScannerStuff();
            }
     
        }
     
        public static void main(String[] args) {
     
            Main2 m = new Main2();
            m.ScannerStuff();
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: checking if it is a character?

    This is an excellent an idea! thank you very much was exactly what i was looking for thank you!

  8. #8
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: checking if it is a character?

    alright last bit, extremely basic i know but i've forgotten.

    i want it to check if the value entered is withing the boundaries.

    for example i want in common language to be

    if the entered value is greater than 0 and less than fourty then{
    }

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Cool Re: checking if it is a character?

    Quote Originally Posted by luke View Post
    alright last bit, extremely basic i know but i've forgotten.

    i want it to check if the value entered is withing the boundaries.

    for example i want in common language to be

    if the entered value is greater than 0 and less than fourty then{
    }
    This can easily be done with an if statement:

                    if(yourInt > 0 && yourInt <= 40){
                        System.out.println("Your value is greater than 0 and less or equal to 40");
                    }

    This is basically saying, if yourInt is greater than 0 and less or equal to 40 then print message.

    The > && <= bits are called Operators. Here is a quick overview:

    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  10. #10
    Junior Member luke's Avatar
    Join Date
    May 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: checking if it is a character?

    thank you.

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: checking if it is a character?

    Quote Originally Posted by luke View Post
    This is an excellent an idea! thank you very much was exactly what i was looking for thank you!
    Brilliant! Glad I could help Luke.

    If you have no further questions, please can you mark this thread as solved. (Check the link in my signature)

    Thanks!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM
  2. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM