How to automatically convert input characters to uppercase letters in Java

I'm using the scanner class to get input and want to convert the input to uppercase letters when displayed This is my code

Scanner input = new Scanner(system.in);
System.out.print("Enter a letter: ");
char c = input.next().charAt(0);
Character.toUpperCase(c);

Because I've converted it to uppercase, but the output looks like

input: a
c = A;
output: Enter a letter: a

PS: the letter "a" is what I input at the terminal

But I want it to appear in uppercase How can I change it?

Solution

Touppercase method does not change the value of char (it cannot); It returns capital letters change

Character.toUpperCase(c);

to

c = Character.toUpperCase(c);

UPDATE

The updated question now shows that when you enter uppercase characters, they will be printed Java cannot do this because Java cannot control how O / s responds to user input to the screen My above solution will only produce additional output, even if it is capitalized

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>