Forums Register Login

Use of Scanner with primitive type char

+Pie Number of slices to send: Send
I need help using the instance of scanner to declare a primitive type char. So far I have declared and instantiated the scanner, and I tested the program using more basic primitive types (i.e. int, double, and short)....

System.out.println("Enter an int c")
int c = sc.nextInt();

System.out.println("Enter a double d")
double d = sc.nextDouble();

System.out.println("Enter a short e")
short e = sc.nextShort();

I just don't know what to use for char. Could anyone please help? Thanks.
+Pie Number of slices to send: Send
I've not used the Scanner class, but if there's nothing standard, this might do

char x = sc.next().charAt(0);
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 940 times.
Similar Threads
a char and int interconversion doubt!
Stuck on Homework
Why does my 'd' variable stay at zero?
Primitives and literals
Whizlabs SCJA Prep Kit (Could just be a clarification problem)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:32:06.