Uppercase a string input from scanner in java

老子叫甜甜 提交于 2019-12-25 08:35:59

问题


I am supposed to use the Scanner in Java to receive a 14 char input and have all the letter char output in uppercase format. I've tried entering some of the code I found through Google such as " str.toUpperCase(Locale.ENGLISH); " but it tells me str cannot be resolved and that the locale can't be resolved. When I did a search on here I got results for uppercasing specific char or counting char. I just need the specific line of input to be uppercased.

The following is what I have at the moment. I am using eclipse java neon

  import java.util.Scanner;

public class MemberIDLotz {
    // Everette Lotz
    public static void main(String[] args) {
        Scanner string = new Scanner(System.in);

        // We're going to get a 14 char string from a user.
        System.out.println("Please enter in a 14 character long ID");
        String name = string.next();
        str.toUpperCase(Locale.ENGLISH);

        string.close();
    }
}

Thank you for your help in advance.

*Edit: Ok so it is no longer giving me an error message, and yes I had simply copy and pasted the code. However when it prints out its not in upper case format. I put the following right after "String name =...." and deleted the str.toUppercase() line:

System.out.println(name);
name = name.toUpperCase();`

回答1:


Is there a reason you don't uppercase immediately?

String name = string.next().toUpperCase()



回答2:


Change to

name = name.toUpperCase(Locale.ENGLISH);

or more simply

name = name.toUpperCase();



回答3:


You can also add trim to it if you'd like, it is a working example:

String choice;

Scanner s = new Scanner(System.in);
choice = s.nextLine().trim().toUpperCase();


来源:https://stackoverflow.com/questions/42016564/uppercase-a-string-input-from-scanner-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!