+ 2

How to get a single character as user input?

27th Jan 2019, 12:54 AM
Nisarga Gangadkar D C
Nisarga Gangadkar D C - avatar
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.
28th Jan 2019, 11:32 PM
Adrien Zettl
Adrien Zettl - avatar
+ 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', '!'}
27th Jan 2019, 7:20 AM
Seb TheS
Seb TheS - avatar
+ 2
This could work: Scanner input = new Scanner (System.in); char c = input.next (); next() takes not the whole line.
27th Jan 2019, 12:32 PM
Denise Roßberg
Denise Roßberg - avatar
0
what languaje?
27th Jan 2019, 1:52 AM
Chris Codes
0
Java: import java.util.Scanner; Scanner input = new Scanner (System.in); int number = input.nextInt (); String text = input.nextLine (); double --> nextDouble() long --> nextLong ();
27th Jan 2019, 3:00 AM
Denise Roßberg
Denise Roßberg - avatar