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

Thread: InputMismatchException

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default InputMismatchException

    The only data type I'm using is int - so I don't see where this error is coming from. I use the same format in a previous section and I get no errors.

    package graduationplanner;
    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class GraduationPlanner {
     
        public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           final int MINCU = 12;
           final int TUITIONCOST = 2890; //stores value for tuition cost per term
           final int TERMMONTHS = 6; //stores value for number of months in a term
           int sum = 0;
     
           System.out.println("Welcome to the Western Governor's University Graduation Planner! \nWe will calculate the number of terms you have left until graduation, \nalong with the amount of tuition you have left to pay. \nWe will also provide the number of months until graduation! \nLet's get started by asking a few questions.");
           	System.out.println("How many CU's are required for your degree?"); //Prompts user for the total number of CU's for degree and stores in variable
           		int totalCU = in.nextInt(); //storing user input
           		if (totalCU <= 0){ //verification on positive input by user
           			System.out.println("That's not right! Enter a number higher than zero!");
           			System.out.println("How many CU's are required for your degree?");
           			totalCU = in.nextInt(); //stores corrected number
           		}
           		//System.out.println(totalCU); //Outputting number entered for check
     
            ArrayList<Integer> leftCU = new ArrayList<>();
            {
                System.out.println("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done.");
                leftCU.add(in.nextInt());
                while (in.hasNextInt()){
                    leftCU.add(in.nextInt());}
                for(int i=0; i < leftCU.size(); i++){
                	sum = sum + leftCU.get(i);}
            }
            //System.out.println(leftCU);
            	System.out.println(sum);
     
           System.out.println("How many CU's are you planning on taking per term?"); //Prompts user for the number of CU's per term
           		int perTerm = in.nextInt(); //storing user input
           			if (perTerm < MINCU){ //verification on positive input by user
           				System.out.println("12 CU's are required per term. Try a higher number!");
           				System.out.println("How many CU's are required for your degree?");
           				perTerm = in.nextInt();} //stores corrected number
           			System.out.println(perTerm);
     
     
           int termLeft = sum/perTerm; //Calculates number of terms left
           int totalTuition = termLeft * TUITIONCOST; //Calculates the amount of tuition left to pay
           int monthsLeft = termLeft * TERMMONTHS; //Calculates the number of months left
     
           System.out.println("+----------------------------------------+");
           System.out.println("|Number of Terms left is: " + termLeft + "|");
           System.out.println("|Total amount of Tuition left is: " + totalTuition + "|");
           System.out.println("|Number of Months left is: " + monthsLeft + "|");
           System.out.println("+----------------------------------------+");
           System.out.println("Thank you for using the WGU Graduation Planner");
           }
        }

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at graduationplanner.GraduationPlanner.main(Graduatio nPlanner.java:37)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: InputMismatchException

    What did the user enter? Can you copy the contents of the command prompt window and paste it here?

    BTW The formatting of the code is very sloppy making it hard to read.
    Also there are a lot of }s hidden at the end of other statements that make the code hard to understand. Please put the }s on a separate line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: InputMismatchException

    Post a sample run so that we can duplicate the error.

    Never mind - but please do so in the future.

    You ask the user to enter a 'Q' to end. What's going to happen when the program is looking for an int but a 'Q' is entered? Hint: You already know the answer.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    mrivera85 (July 4th, 2014)

  5. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: InputMismatchException

    Quote Originally Posted by GregBrannon View Post
    Post a sample run so that we can duplicate the error.

    Never mind - but please do so in the future.

    You ask the user to enter a 'Q' to end. What's going to happen when the program is looking for an int but a 'Q' is entered? Hint: You already know the answer.
    Thanks - I guess because the output began with the next question and the error was also in the next section, I just assumed it had to do with that one, not the previous one. When I removed the whole previous section, it worked. Thanks again. Very new to this.

Similar Threads

  1. Solve InputMismatchException
    By idkwtp100 in forum Exceptions
    Replies: 0
    Last Post: February 28th, 2013, 01:28 AM
  2. nextInt - can not find InputMismatchException
    By Samaras in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 18th, 2012, 06:47 PM
  3. 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
  4. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 10:30 AM
  5. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 01:02 AM