-1

I am very new to Java and struggling to complete an assignment. I have read several posts but can't seem to figure out how to add the "Enter·y·to·play·again,·n·to·quit(y/n)" into my code. Here is what I have so far. Any direction would be appreciated. I can't figure out how to get it to return to Enter one number. THX

//Program to display Even or Odd Integer
import java.util.Scanner;

public class EvenOdd {
    public static void main(String[] args) { //method to determine is a number is even or odd
        Scanner input = new Scanner(System.in);
        int number; //number o be entered
        System.out.print("Enter one number: "); //prompt user to enter a number
        number = input.nextInt();
        switch (number % 2) {
            case 0:
                System.out.printf("%d is even\n", number);
            case 1:
                System.out.printf("%d is odd\n", number);
        }
        System.out.print("Enter Y to play again, N to quit: "); //prompt user to enter a number
        number = input.nextInt();
    }

    public boolean isEven(int number) {
        return number %2 == 0;
    } //end method isEven
} //end class EvenOdd

Result:

Enter one number: 1
1 is odd
Enter Y to play again, N to quit: n
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 EvenOdd.main(EvenOdd.java:21)
Abra
  • 11,631
  • 5
  • 25
  • 33
Snowbrd1
  • 15
  • 4
  • You need a loop and for the y/n question you must read a string – Joakim Danielson Nov 23 '19 at 16:59
  • You print "Enter Y to play again, N to quit" then you call `nextInt()`. Why would you believe that an `int` can accept a `Y` or `N` character? – Andreas Nov 23 '19 at 17:02
  • _Y_ and _N_ are not numbers. `InputMismatchException` means you want the user to enter a number but she entered a letter instead. You need to use method `next()` in order to accept a _Y_ or _N_ from the user. – Abra Nov 23 '19 at 17:02
  • @Abra If you use `nextLine()`, you run into this problem: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/5221149) – Andreas Nov 23 '19 at 17:03
  • Thank you. Being very new to programming the amount of information is overwhelming and the smallest things can be very frustrating. I can see the running theme in the replies about the mismatch (being shown in the error). I knew it was related but without the experience and couldn't understand the fix. I think I got it - see below. Thank you again for the quick replies. There may be more questions to follow in the future. – Snowbrd1 Nov 23 '19 at 17:28
  • I've been looking through this site for the past few hours to help myself understand what is required when posting questions and what effect it has on posting questions in the future. I thought that I followed the criteria but looks like it was down voted for lack of research or the question not being clear or useful. @Andreas I was wondering if you might provide some feedback so that I could better present my questions in the future. Thanks again for your assistance. – Snowbrd1 Nov 23 '19 at 21:20

1 Answers1

0

The exception you are getting tells you that the input did not match with the correct type you're trying to assing. Basically: you are trying to assign a type String (e.g. "Y"/"N") to an int. This will throw an error. Read more on different types here: https://en.wikibooks.org/wiki/Java_Programming/Primitive_Types

Below is a suggestion on how to fix this. You will also notice that I added a while loop to your program to actually be able to play again. The code you want to change is input.nextInt() to input.next(). You also want to change which variable you assign it to, because you cannot assign a String to an int. Also added break; to your switch statement, so it will not go through each case.

public class Main {

    public static void main(String[] args) { //method to determine is a number is even or odd
        String cont = "Y";
        Scanner input = new Scanner(System.in);
        while (cont.equals("Y")) {
            int number; //number o be entered
            System.out.print("Enter one number: "); //prompt user to enter a number
            number = input.nextInt();
            switch (number % 2) {
                case 0:
                    System.out.printf("%d is even\n", number);
                    break; //NOTICE here
                case 1:
                    System.out.printf("%d is odd\n", number);
                    break; //NOTICE here
            }
            System.out.print("Enter Y to play again, N to quit: "); //prompt user to enter a number
            cont = input.next(); // <---- This is what you want 
        }
        System.out.println("Did not want to play again, exiting...");
    }
}
Selaka Nanayakkara
  • 2,198
  • 14
  • 30
  • Thank you. From the initial replies I was able to understand the error but not the fix. A number converting to a letter didn't make sense. Your input was very useful: – Snowbrd1 Nov 23 '19 at 17:32
  • Enter one number: 6 6 is even Enter y to play again, n to quit(y/n): y Enter one number: 5 5 is odd Enter y to play again, n to quit(y/n): n User did not want to continue... – Snowbrd1 Nov 23 '19 at 17:32