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

Thread: Getting InputMismatchException

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Getting InputMismatchException

    Lumumba is an entrepreneur based in eMalahleni. He has opened a sphaza shop called EveryThingFishy. The shop specializes in selling fast food related to fish. Table 1 below shows the menu they currently sell.

    No. Menu Amount (R)
    1 Large Hake Fish 100.00
    2 Fish Burger 85.00
    Their large hake fish is served with either salads or chips. The fish burger is either served as spicy or regular. As a starting company, they charge a small fee for the extras. The table below shows the amount charged for the extras. There’s no extra charge for the regular option.

    No. Extra Amount (R)
    1 Salads 10.00
    2 Chips 5.00
    3 Spice 2.00
    Create an application for Lumumba that will help him run the business. The application must allow a customer to choose a menu item to buy. If an invalid menu code is entered, the application must display an error message and come to an end. If the user enters a valid menu code, the customer must be allowed to select the extras relating to that menu. The amount due must be determined and displayed. The application must give the customer an opportunity of making payment. If payment is insufficient, the application must display an error message and thereafter come to an end. If payment is valid, change must be determined and displayed. Lastly, the application must display the details of the food bought.

    Here is my code:

    package everythingfishyapp;
    import java.util.Scanner;
     
    /**
     *
     * @author SETH
     */
    public class EveryThingFishyApp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // declare variables
            final double AMOUNT1 = 100.00, AMOUNT2 = 85.00, AMOUNT3 = 10.00,
            AMOUNT4 = 10.00,AMOUNT5 = 10.00 ;
            double amount,amtDue,change,payment;
            String menu1="Large Hake Fish", menu2="Fish Burger",
            extra1="Salads",extra2="Chips",extra3="Spice";
            final int CODE1=1, CODE2=2;
            int code,extra, extr1=1,extr2=2,extr3=3;
     
            Scanner sc = new Scanner(System.in);
     
            //prompt the user to choose a menu item to buy
            System.out.println("Please enter a menu item: ");
            code=sc.nextInt();
            System.out.println("Please select an extra: ");
            extra=sc.nextInt();
            amount = sc.nextDouble();
            System.out.println("Please enter a payment of the menu item and the extra: ");
            payment = sc.nextDouble();
     
     
     
            //determine the amount due
            amtDue = code + extra;
     
            //determine the change
            change = payment - amount;
     
     
            if((code==CODE1)||(code==CODE2) && (extra==extr1)||(extra==extr2)||(extra==extr3) && (payment >= 87.00) ){
                            System.out.println("Your menu item is: " + code + " and your extra is: "+ extra + "\n" +
                            "you owe: " + code + extra + "The change is: " + (payment - amount));
                    }
     
            }
     
     
     
     
     
     
     
        }

    here is the output:
    run:
    Please enter a menu item: 
    2
    Please select an extra: 
    2


    --- Update ---

    Later this year local government elections will be held in South African. Political parties are expected to register their candidates with the IEC (Independent Electoral Commission). The IEC requires parties to submit lists of candidates meeting the following basic requirements:

    1. Candidates must be South Africans; and
    2. Candidates must be 18 years old and above.
    Create an application that will help parties check if their candidates meet the minimum requirements of IEC .

    here is my code:
     
    package candidateapp;
    import java.util.Scanner;
     
    /**
     *
     * @author SETH
     */
    public class CandidateApp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // declare variables
            String natnlity;
            int age;
     
            Scanner sc = new Scanner(System.in);
     
            //prompt the user to enter his nationality
            System.out.println("Please enter your nationality: ");
            natnlity = sc.next();
            System.out.println("Please enter your age: ");
            age = sc.nextInt();
     
            //check if the candidates meet the minimum requirements
            if((natnlity=="south african")&& (age >= 18)){
                System.out.println("You qualify for the elections");
            }else{
                System.out.println("You don't qualify fot the elections");
     
     
        }
     
    }
        }

    here is the output

     
    Please enter your nationality: 
    south african
    Please enter your age: 
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:864)
    	at java.util.Scanner.next(Scanner.java:1485)
    	at java.util.Scanner.nextInt(Scanner.java:2117)
    	at java.util.Scanner.nextInt(Scanner.java:2076)
    	at candidateapp.CandidateApp.main(CandidateApp.java:29)
    Java Result: 1

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

    Default Re: Getting InputMismatchException

    What value is the user entering when asked for age? The nextInt method requires a valid number.
    If the user enters a non-numeric value, there will be an InputMismatchException.

    The next method only reads ONE token from the typed in input: south african
    It would read south and leave african in the input buffer. When nextInt is called it would read african which is not a number.
    If there is more than one token to be read for a value, use the nextLine method to read everything the user enters up to the press of the Enter key.

    To see what happened print out the value in natnlity
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to fix InputMismatchException?
    By kukupie123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2019, 12:21 PM
  2. [SOLVED] InputMismatchException
    By mrivera85 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2014, 11:49 AM
  3. Solve InputMismatchException
    By idkwtp100 in forum Exceptions
    Replies: 0
    Last Post: February 28th, 2013, 01:28 AM
  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