简体   繁体   English

使用.toUpperCase()编译代码时,获取“字符不能取消引用错误”;

[英]Getting a 'char cannot be dereferenced error' when compiling code using .toUpperCase();

im new to java and am writing a program that will let you input your first and last name and will give you your initials, but i want the intitials to always be in upper case. 我是java的新手,正在编写一个程序,该程序可以让您输入名字和姓氏,并为您提供首字母缩写,但是我希望initiinal始终使用大写形式。

I get a "char cannot be dereferenced" error whenever i run the code. 每当我运行代码时,都会出现“无法取消引用字符”错误。

    import java.util.*;

    public class InitialHere
    {
        public static void main (String[] args)
        {
            Scanner getInput = new Scanner (System.in);
            String firstName;
            String lastName;
            char firstInitial;
            char lastInitial;

            System.out.println("What is your first name?");
            System.out.println();
            firstName = getInput.nextLine();
            System.out.println();
            System.out.println("Thankyou, what is your last name?");
            System.out.println();
            lastName = getInput.nextLine();

            firstInitial = firstName.charAt(0);
            lastInitial = lastName.charAt(0);
            firstInitial = firstInitial.toUpperCase();
            lastInitial = lastInitial.toUpperCase();

            System.out.println();
            System.out.println("Your initials are " + firstInitial + "" + lastInitial + ".");

        }
    }

In Java, primitives have no methods, only their boxed types do. 在Java中,原语没有方法,只有装箱的类型才有。 If you want to get the uppercase version of a char , the Character class has a method just for that: Character.toUpperCase : 如果要获取char的大写版本,则Character类具有一个专门用于该方法的方法: Character.toUpperCase

 firstInitial = Character.toUpperCase(firstInitial);

(Do the same for lastInitial ) (对lastInitial执行相同lastInitial

firstInitial is of type char which has no method toUpperCase . firstInitialchar类型的,没有toUpperCase方法。 (Being a primitive, it has no methods at all.) Call toUpperCase on the original String instead. (作为基本体,它根本没有任何方法。)相反,请对原始String调用toUpperCase

Update (see comments): The documentation of Character.toUpperCase says: 更新(请参阅评论): Character.toUpperCase的文档说:

In general, String.toUpperCase() should be used to map characters to uppercase. 通常,应使用String.toUpperCase()将字符映射为大写。 String case mapping methods have several benefits over Character case mapping methods. String大小写映射方法比Character大小写映射方法具有多个优点。 String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping methods cannot. String大小写映射方法可以执行区域设置敏感的映射,上下文敏感的映射和1:M字符映射,而Character大小写映射方法则不能。

Therefore, the most robust approach would be to use 因此,最可靠的方法是使用

String firstName = firstName.trim();  // ignore leading white space
final String firstInitial = firstName.substring(0, firstName.offsetByCodePoints(0, 1)).toUpperCase();

Another solution: 另一个解决方案:

firstInitial = (char) (firstInitial >= 97 && firstInitial <= 122 ? firstInitial - 32 : firstInitial);
lastInitial = (char) (lastInitial >= 97 && lastInitial <= 122 ? lastInitial - 32 : lastInitial);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM