-1

I am having trouble with understanding what is wrong with this code. On Dr.Java everything is working fine but on another code running platform called edhesive (which is where i was assigned this project) it is giving me an error. I have checked everything that i could think is going wrong but still don't know what is wrong.

import java.util.Scanner;

public class Main
{
     public static void main(String[] args)
     {
          Scanner scan = new Scanner(System.in);
          System.out.println("Welcome. What is your name?");
          String name = scan.next();

          System.out.println("Hello " + name + ". Try your best to crack the code!");

          System.out.println("PHASE 1");
          System.out.println("Enter a number:");
          int phaseOneNum = scan.nextInt();

          if (phaseOneNum == 3)
          {
               System.out.println("Correct!");

               System.out.println("PHASE 2");
               System.out.println("Enter a number:");
               int phaseTwoNum = scan.nextInt();

               if (phaseTwoNum == 1 || (phaseTwoNum > 33 && phaseTwoNum<= 100))
               {
                    System.out.println("Correct!");

                    System.out.println("PHASE 3");
                    System.out.println("Enter a number:");
                    int phaseThreeNum = scan.nextInt();

                    if (phaseThreeNum > 0 && ((phaseThreeNum % 3 == 0) || (phaseThreeNum % 7 == 0)))
                    {
                      System.out.println("Correct!");
                      System.out.println("You have cracked the code!");
                    }

                    else
                    {
                         System.out.println("Sorry, that was incorrect!");
                         System.out.println("Better luck next time!");
                    }
               }

               else
               {
                    System.out.println("Sorry, that was incorrect!");
                    System.out.println("Better luck next time!");
               }
          }

          else
          {
               System.out.println("Sorry, that was incorrect!");
               System.out.println("Better luck next time!");
          }
     }
}

After running it on edhesive, I get this error

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 Main.main(Main.java:184)
    at Ideone.test(Main.java:111)
    at Ideone.test(Main.java:31)
    at Ideone.main(Main.java:23)

Can someone help me please?

  • 1
    put a breakpoint on line 184 and invoke Scanner.nextInt, see the result. – Roman C Oct 14 '17 at 14:45
  • Here Some Questions in SO may help you to understand this exception: https://stackoverflow.com/questions/13408799/what-is-a-java-util-inputmismatchexception https://stackoverflow.com/questions/19460363/scanners-exception-java-util-inputmismatchexception – JHDev Oct 14 '17 at 14:49

1 Answers1

0

Run Time Error — Exception in thread “main” java.util.InputMismatchException

scan.nextInt(); accept integer only, You may passing String. So, It gives you java.util.InputMismatchException

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome. What is your name?");
        String name = scan.next();

        System.out.println("Hello " + name + ". Try your best to crack the code!");

        System.out.println("PHASE 1");
        System.out.println("Enter a number:");
        int phaseOneNum = 0;
        do {
            while (!scan.hasNextInt()) {
                System.out.println("That's not a number!");
                scan.next(); // this is important!
            }
            phaseOneNum = scan.nextInt();
        } while (phaseOneNum <= 0);

        if (phaseOneNum == 3) {
            System.out.println("Correct!");

            System.out.println("PHASE 2");
            System.out.println("Enter a number:");
            int phaseTwoNum;
            do {
                while (!scan.hasNextInt()) {
                    System.out.println("That's not a number!");
                    scan.next(); // this is important!
                }
                phaseTwoNum = scan.nextInt();
            } while (phaseTwoNum <= 0);

            if (phaseTwoNum == 1 || (phaseTwoNum > 33 && phaseTwoNum <= 100)) {
                System.out.println("Correct!");

                System.out.println("PHASE 3");
                System.out.println("Enter a number:");
                int phaseThreeNum;
        do {
            while (!scan.hasNextInt()) {
                System.out.println("That's not a number!");
                scan.next(); // this is important!
            }
            phaseThreeNum= scan.nextInt();
        } while (phaseThreeNum<= 0);

                if (phaseThreeNum > 0 && ((phaseThreeNum % 3 == 0) || (phaseThreeNum % 7 == 0))) {
                    System.out.println("Correct!");
                    System.out.println("You have cracked the code!");
                } else {
                    System.out.println("Sorry, that was incorrect!");
                    System.out.println("Better luck next time!");
                }
            } else {
                System.out.println("Sorry, that was incorrect!");
                System.out.println("Better luck next time!");
            }
        } else {
            System.out.println("Sorry, that was incorrect!");
            System.out.println("Better luck next time!");
        }
    }
}
  • How would I fix that and make sure that my program isn't passing a string – Ricky Estrella Oct 14 '17 at 15:01
  • I do test at my side works fine for me, Did you test your program by passing proper `integer value` for your program. –  Oct 14 '17 at 15:03
  • Yes I have. I have ran this program many times on Dr. Java and it works perfectly fine when use proper integer values. Do you know how I can prevent strings from being passed? – Ricky Estrella Oct 14 '17 at 15:07
  • @RickyEstrella Answer is update for `how I can prevent strings from being passed? ` –  Oct 14 '17 at 15:39