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

Thread: nextInt - can not find InputMismatchException

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default nextInt - can not find InputMismatchException

    I am trying to read three integers from user.However,as always,i have to check if the input is ok.So i wrote the code,but can not find a way to ensure that the input is ok.InputMismatchException seems to suit but when i write her i get the error : Uncompilable source code - cannot find symbol

    Here is my code.What am i missing? :/

    static void getInput(int[] input) {
            System.out.print("Please enter length of phrase,minimum number of words"
                    + " of the essay and maximum number of words : ");
     
            try{
                Scanner in = new Scanner(System.in);
                for(int i=0 ; i<input.length ; i++)
                    input[i] = in.nextInt();
            } catch (InputMismatchException e) {
                System.err.println("Unexpected IO ERROR: " + e.toString());
            }
     
        }

    PS1- yes you guess it right i am a newbie,so every tip is a treasure for me
    PS2 - how can i wrap my code to something that colours the functions etc.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: nextInt - can not find InputMismatchException

    You didn't post the import statements for your code so we can't see what you are using.

    To find what package a class is in, look it up in the API doc:
    Java Platform SE 7

    At the top of the page for the class will be its definition showing what package it is in.
    For example InputStream has this at the top:
    java.io.InputStream
    showing that InputStream is in the java.io package. You would use that in the import statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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: nextInt - can not find InputMismatchException

    To have the forum highlight/color your code, specify a language. For example:

    [code=java]//your code goes here[/code]

    This looks like:
    //your code goes here

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: nextInt - can not find InputMismatchException

    Here is the whole code
    package essaycorrection;
     
    import java.util.Scanner;
     
    public class EssayCorrection {
     
        public static void main(String[] args) {
            int[] input = new int[3];//N,minWords,maxWords
            getInput(input);
            Phrase test = new Phrase(input[0]);
     
        }
     
        static void getInput(int[] input) {
            System.out.print("Please enter length of phrase,minimum number of words"
                    + " of the essay and maximum number of words : ");
     
           // try{
                Scanner in = new Scanner(System.in);
                for(int i=0 ; i<input.length ; i++)
                    input[i] = in.nextInt();
    //        } catch (InputMismatchException e) {
    //            System.err.println("Unexpected IO ERROR: " + e.toString());
    //        }
     
        }
    }
    Still can not find it,even after looking at the link :/

  5. #5
    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: nextInt - can not find InputMismatchException

    Here's the Javadoc for InputMistmatchException

    Pretty much the first line tells you which package this class is in. This is right above the class name and inheritance hierarchy. You need to import the class before you can use it.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: nextInt - can not find InputMismatchException

    Duplicate link,but you did good to repost in order to emphasize the importance of it So what was missing was this line
    [code=java]import java.util.InputMismatchException;[code]
    So now i got it together,and my goal is that when the user inputs some letters instead of numbers then the user will be able to input again and so on.
    static void getInput(int[] input) {
            boolean error = false;
            System.out.print("Please enter length of phrase,minimum number of words"
                    + " of the essay and maximum number of words : ");
            do {
                try {
                    Scanner in = new Scanner(System.in);
                    for (int i = 0; i < input.length; i++) {
                        input[i] = in.nextInt();
                    }
                } catch (InputMismatchException e) {
                    System.err.println("Unexpected IO ERROR: " + e.toString());
                    error = true;
                }
            } while (error);
     
        }

    So i typed "dd" ,exception was thrown as it was expected but then nothing happens.What i am missing again? :/

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: nextInt - can not find InputMismatchException

    exception was thrown as it was expected but then nothing happens
    Please explain. What do you expect to happen?

    Please post the contents of the console from your test.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: nextInt - can not find InputMismatchException

    Can't find the icon but i copy pasted the output.(from netbeans)
    run:
    Please enter length of phrase,minimum number of words of the essay and maximum number of words : dd
    Unexpected IO ERROR: java.util.InputMismatchException
    5 5 5
    BUILD STOPPED (total time: 11 seconds)
    What i expect to happen is this : as long as the input is not ok,i want to get the user input again.So when the exception is thrown this means something is not ok so re-input!

    Idea->Does the exception makes the stream to close?That is what is happening?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: nextInt - can not find InputMismatchException

    Does the exception makes the stream to close?That is what is happening?
    Why do you think that? What happened to the input: 5 5 5? Did the program read that OK?

    Try debugging the code by printing out each number after it is read. Be sure to include an ID String with the printout:
    S.o.println("read: " + input[i] + ", i=" +i);

    Also look at how you use the boolean: error
    when is it false and when is it true?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: nextInt - can not find InputMismatchException

    Yes it is ok.I found what was wrong.The boolean error was not reseted to false! Thank you very much for your answers

Similar Threads

  1. how to use nextInt() to generate random integers?
    By rph in forum Object Oriented Programming
    Replies: 6
    Last Post: January 3rd, 2013, 02:32 AM
  2. using java.util.InputMismatchException with try
    By itayj in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 26th, 2011, 06:24 PM
  3. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 10:30 AM
  4. nextint() Method !
    By M7MD in forum Object Oriented Programming
    Replies: 11
    Last Post: October 27th, 2010, 11:47 AM
  5. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 01:02 AM