as we know the code of scanner is like this,

Scanner scan = new Scanner(System.in);
System.out.print("something is something: ");
[type] [variable name] = scan.next();...

so here is my problem, how can we scan char type?

5 Years
Discussion Span
Last Post by stevanity

You mean a single character?

To do that you can do this:

Character char=scan.next().charAt(0);

And BTW, scan.next() returns only a string type..
The prototype is....

public String next()

For other types you have custom methods like nextInt(), nextDouble(), etc.. But nothing for char.

So use above method for getting a char.


Ok. Let me split that up for you....

String in=scan.next();

This gets a string for you. In our case the user will input only one character but still its a String object.

Now to get a certain Character/char from a String, we can use charAt(int) method of String which returns the character at the specified index.

So since our required character is at the first position of the String (in), we use '0' as argument to charAt()


char character=in.charAt(0);

What I did was, I combined all this into one statement this way,

char character=scan.next().charAt(0);

Oh, which mean charAt(0) can get the first character of a sentence. :)

oh well, how about JOptionPane?
i mean convert it to character also, after input.?


Yes, the result string from a JOptionPane can also be converted to char. ANY STRING CAN BE USED TO EXTRACT A CHAR...

If youve got ur problem solved, please mark this thread as solved! :)


but how to convert into character type? i've been tried many times also can't work it. :(

char character = Character.parseChar(string);

where's wrong? :o

Edited by gahhon: n/a


The String method charAt(int position) returns a single char, there's no conversion needed. What didn't you understand about Stevanity's posts?


i understand what he explain to me, but i though that method also can convert into char instead of using charAt(0). :):)


A String contains many chars, so you need a method like charAt that allows you to specify which char you want. There's no general way to turn a whole String into one char - eg supposing there were such a method, what would you expect Character.parseChar("ABCDEF") to return?


i understand what he explain to me, but i though that method also can convert into char instead of using charAt(0). :):)

Do u know what a "Char" is?

And BTW, why do u not want to use charAt(0)?? And what are u going to do with the character? If u are just going to do some comparisons and if ure sure the user will enter only a character u can leave it as a string itself.. (with one character)


Just a question: since you are reading from an ASCII 1-byte-per-character stream, what's wrong with
char c = scan.nextByte();


I dont think java will allow you do that with its "type constraints"
And also character is stored as 2 Byte Unicode.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.