0
how to do this?
how can make the program to make an exception when you write a String in a Scanner that is used for a char. for example: Scanner s = new Scanner (System.in); char myChar = s.next().charAt(0); //i can write a String in that Scanner and nothing will happen... i thought about doing this: if (myChar.length() > 0){...} or: if (Character.length(myChar) > 0){...} non of them work.. how can i make it work?
1 Respuesta
0
If you scan in like that it will always be 1 char unless there is no input.
Scan in the full string
Scanner sc = new Scanner (System.in);
String s = sc.next();
char c = s.charAt();
System.out.println("c = "+c);
System.out.println("l = " + s.length());
this will let you check the length of the input but still use it as a char and check the length of the input