Friday, June 28, 2019

java - Making a concat operator to add char array to a string. Printing a reference variable of a string object

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
}
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...