【问题标题】:Making a concat operator to add char array to a string. Printing a reference variable of a string object制作一个 concat 运算符以将 char 数组添加到字符串。打印字符串对象的引用变量
【发布时间】:2012-11-02 12:53:17
【问题描述】:

到目前为止,如果我尝试使用 concat 运算符,我会收到此错误 Tester.java:14:找不到符号 符号:方法 concat(MyString) 位置:MyString 类 System.out.println(hello.concat(再见)); // 打印“hellogoodbye”

当我尝试打印 MyString 的“hello”对象时,我得到了 MyString@558ee9d6 我觉得它离工作太近了..

public class MyString

{
     private char[] charString;
     private String oneString;


      public  MyString(String string) 
      {
        this.oneString = string;


      }

//second constructor for overloading
    public MyString(char[] s) 
    {

        this.charString = s;
        this.oneString = charString.toString();
    }

//methods
    public String toString( char [] s)
    {
        return new String(s);
    }

    public char charAt(int i) {

        char [] temp = new char[oneString.length()];
        for ( int j = 0; j < oneString.length() ;  j++)
        temp[j] = oneString.charAt(i);
        return temp[i];
    }

    public String concat ( char[] s)
    {
        s.toString();
        String result = oneString + s;
        return result;
    }

    public String concat ( String s)
    {
        String result = oneString + s;
        return result;
    }


}

公共类测试员 { public static void main (String[] args)

    {
    MyString hello = new MyString("hello");
    System.out.println(hello);  // right now this prints MyString@558ee9d6
    System.out.println(hello.charAt(0));    // works, prints 'h'
    char[] arr = {'g','o','o','d','b','y','e' };
    MyString goodbye = new MyString(arr);
   // System.out.println(hello.concat(goodbye)); // i can't get this line to work
    System.out.println(hello.equals(goodbye)); // works, prints false
    System.out.println(hello.equals(hello));  //works, prints true
    }
    }

【问题讨论】:

  • 你应该在发布之前稍微校对你的代码。一个简单的编译可以帮助你解决很多问题。
  • 内部charString 是必需的吗?它只是让你的内部状态复杂化,只保留一个String 会更简单。

标签: java arrays


【解决方案1】:

您正在尝试打印一个对象:

System.out.println(hello);  // right now this prints MyString@558ee9d6

在这种情况下,您的 MyString 类

为你的变量 oneString 设置 get 方法。

public String getOneString() {return this.oneString;}

然后调用

System.out.println(hello.getOneString());

另一个问题

System.out.println(hello.concat(goodbye));

你的 concat 方法接收一个字符串而不是一个 MyString 类

你可能想这样做

System.out.println(hello.concat(goodbye.getOneString()));

或

public String concat ( MyString myS)
    {
        String s = myS.getOneString();
        String result = oneString + s;
        return result;
    }

最终结果:

public class Tester { public static void main (String[] args)

    {
    MyString hello = new MyString("hello");
    System.out.println(hello.getOneString());
    System.out.println(hello.getOneString().charAt(0)); 
    char[] arr = {'g','o','o','d','b','y','e' };
    MyString goodbye = new MyString(arr);
    System.out.println(hello.concat(goodbye.getOneString()));
    System.out.println(hello.equals(goodbye)); // works, prints false
    System.out.println(hello.equals(hello));  //works, prints true
    }
    }

【讨论】:

    【解决方案2】:

    charString.toString() 将为您提供char[] 对象的String 表示,它不会将您的char[] 转换为String。

    如果您希望/需要 oneString 属性包含您的 charString 的 String 表示,那么您可以使用您的 char 数组创建一个 String:

    this.oneString = new String(charString);
    

    还有这一行

    System.out.println(hello);
    

    将打印您的对象的String 表示。您需要在MyString 类中覆盖Object 类中的public String toString() 方法:

    @Override
    public String toString() {
        return this.oneString; //or similar
    }
    

    【讨论】:

    • 你知道为什么我在尝试运行 concat 时会收到“找不到符号”错误吗?
    • @FredV 那是因为你的concat 方法接收char[] 或String 参数,而不是MyString,而你有MyString goodbye。
    【解决方案3】:

    我会说现在,您正在尝试将字符数组添加到字符串中。调用 s.toString() 不会使 char[] 成为字符串。这不会像你拥有的那样工作。试试

    result = oneString + new String(s);
    

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2015-03-08
      相关资源
      最近更新 更多