Friday, 18 November 2016

java - getOption not working, constant error

I am trying to move on in my code and I am getting an error that I don't know how to fix.
The code gets to the point where it asks me for the options, then when I type, I'd like it to print 'this is letter S' or 'this is letter T' based on the input entered by the user. Pls keep in mind two things. 1) I need to use the "char getOption()" method and 2) I am new to this, so bear with me on this. The error I am getting is:



Thanks in advance.



import java.util.Scanner;

public class shapes {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

displayWelcome();
displayMenu();
char resp = getOption();
if( resp == 'S' )
System.out.println("This is letter S");

else if( resp == 'T' )

System.out.println("This is letter T");

}

public static void displayWelcome() {

System.out.println("WELCOME TO THE SHAPE PRINTER!");
System.out.println("-----------------------------");

}



public static void displayMenu() {

System.out.println("Options: ");
System.out.println(" S)quare");
System.out.println(" H) Shape");
System.out.println(" T)riangle");
System.out.println(" X) Shape");
System.out.println(" Q)uit");


}


public static char getOption() {

Scanner input = new Scanner(System.in);

input.nextInt();
char c = input.next().charAt(0);

return c;

}


}



//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 shapes.getOption(shapes.java:60)
// at shapes.main(shapes.java:27)

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...