-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?

2

1 Answer 1

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!");
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

How would I fix that and make sure that my program isn't passing a string
I do test at my side works fine for me, Did you test your program by passing proper integer value for your program.
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?
@RickyEstrella Answer is update for how I can prevent strings from being passed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.