How to convert input char to uppercase automatically in Java

与世无争的帅哥 提交于 2019-11-29 11:54:14

The toUpperCase method doesn't change the value of the char (it can't); it returns the uppercased char. Change

Character.toUpperCase(c);

to

c = Character.toUpperCase(c);

UPDATE

The updated question now indicates that the uppercased characters are to be printed as they're typed. Java cannot do that, because Java doesn't control how the O/S echoes user input to the screen. My solution above would only produce additional output, even if it is uppercased.

System.out.println(Character.toUpperCase(c));

Since, java is pass by value, you need to use the return value. Either print Character.toUpperCase(c) directly or set it to some var.

Here Is An Example Of How You Can Change A Character To UpperCase.

char ch;

    System.out.println("Input Characters:");

    ch = (char) System.in.read();
    System.out.println("Character Is: " + ch);

    sc.nextLine();

    System.out.println("Upper Case: " + Character.toUpperCase(ch));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!