1

So far, if I try to use the concat operator I get this error Tester.java:14: cannot find symbol symbol : method concat(MyString) location: class MyString System.out.println(hello.concat(goodbye)); // prints "hellogoodbye"

and when I try to print the "hello" object of MyString I get MyString@558ee9d6 I feel like its soo close to working..

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 class Tester { 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
    }
    }
2
  • You should probably proofread your code a little bit before posting it. There are a lot of problems that a simple compile could help you with. Commented Nov 14, 2012 at 1:51
  • The internal charString is that required? It's just complicating your internal state, keeping just a String makes it much simpler. Commented Nov 14, 2012 at 1:51

3 Answers 3

1

You are trying to print an Object :

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

In this case your MyString class

Make the get method to your variable oneString.

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

and then call

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

Another problem

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

You concat method receives a string and not a MyString class

You may want to do this

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

or

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

Final 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
    }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

charString.toString() will give you the String representation of the char[] object, it won't convert your char[] into a String.

If you want/need that oneString attribute contains the String representation of your charString, then you could create a String using your char array:

this.oneString = new String(charString);

Also, this line

System.out.println(hello);

would print the String representation of your object. You need to override the public String toString() method in Object class in your MyString class:

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

2 Comments

do you know why does do i get a "cannot find symbol" error for concat when i try to run it?
@FredV that's because your concat method receives a char[] or a String parameter, not a MyString, and you have MyString goodbye.
0

I would say that right now, you are trying to add a character array to a string. Calling s.toString() wouldn't make the char[] a String. That won't work the way you have it. Try

result = oneString + new String(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.