toUpperCase(); - exception:对于Pseudo类型,未定义toUpperCase()方法

时间:2015-10-19 23:37:40

标签: java

这是我的程序的代码。主要方法已提供,无法触及。 main方法将String转换为大写但不接受toUpperCase(); java方法。如何在不改变主方法的情况下解决这个问题?

public class Pseudo {

String name;
char charArray[] = new char[80];
int lenghtOfString =0;
String sArray;

public static void main(String[] args) {

    // Instantiate class        
    Pseudo pseudo = new Pseudo();

    // Verify class
    System.out.println("Verifying Pseudo:");
    pseudo.setString("1234567890 Computer Science !(*@&#)*&");
    System.out.println("pseudo = " + pseudo.getString());
    System.out.println("pseudo.charAt(5) = " + pseudo.charAt(5));
    System.out.println("pseudo.indexOf('C') = " + pseudo.indexOf('C'));
    System.out.println("pseudo.toUpperCase() = " + pseudo.toUpperCase());
    System.out.println("pseudo.substring(11, 19) = " + pseudo.substring(11, 19));
    pseudo.setString("Compare");
    System.out.println("pseudo.equals(\"Compare\") = " + pseudo.equals("Compare"));
    System.out.println("pseudo.equals(\"Compare!\") = " + pseudo.equals("Compare!"));

    // String equivalent (should match!)
    System.out.println("\nComparing String:");
    String string = "1234567890 Computer Science !(*@&#)*&";
    System.out.println("string = " + string);
    System.out.println("string.charAt(5) = " + string.charAt(5));
    System.out.println("string.indexOf('C') = " + string.indexOf('C'));
    System.out.println("string.toUpperCase() = " + string.toUpperCase());
    System.out.println("string.substring(11, 19) = " + string.substring(11, 19));
    string = "Compare";
    System.out.println("string.equals(\"Compare\") = " + string.equals("Compare"));
    System.out.println("string.equals(\"Compare!\") = " + string.equals("Compare!"));
}

public void setString(String s1){
    name=s1;
}
public String getString() {
    return name;
}
public char charAt(int i1) {
    return name.charAt(i1);
}
public int indexOf(char c1) {
    return name.indexOf(c1);
}
public String substring(int start, int end) {
    return name.substring(start,end);
}
public boolean equals(String s2) {
    if (s2 == name){
        return true;
    }
    else {
        return false;
    }
} 
}

据我所知,我可以创建一个名为toUpperCase的方法来运行.toUpperCase方法。但你能解释为什么你必须创建一个运行.toUpperCase()的方法;方法java已经在它的库中了吗?为什么.toUpperCase();方法在主要方面不一样?

2 个答案:

答案 0 :(得分:1)

错误是自我解释的。您的toUpperCase()课程没有方法Pseudo

toUpperCase课程添加方法Pseudo

public String toUpperCase()
{
    return name.toUpperCase();
}

答案 1 :(得分:0)

只需在toUpperCase课程中添加Pseudo方法。