+ 2
How to get a single character as user input?
5 Réponses
+ 3
In Java and if a String fits you, you could perhaps do it like that:
Scanner sc = new Scanner (System.in);
String input = sc.nextLine();
String charac = input.substring(0,1);
If there are no mistakes, no matter how many characters the user inputs, it keeps only the first one.
+ 2
In Java you may not be able to receive single characters with Scanner.
But you can get strings and convert them to char arrays with the toCharArray method:
string.toChatArray()
"Hello world!".toCharArray() == {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'}
+ 2
This could work:
Scanner input = new Scanner (System.in);
char c = input.next ();
next() takes not the whole line.
0
what languaje?
0
Java:
import java.util.Scanner;
Scanner input = new Scanner (System.in);
int number = input.nextInt ();
String text = input.nextLine ();
double --> nextDouble()
long --> nextLong ();