Why Does the Tostring Method in Java Not Seem to Work for an Array

Why does the toString method in java not seem to work for an array

To get a human-readable toString(), you must use Arrays.toString(), like this:

System.out.println(Arrays.toString(Array));

Java's toString() for an array is to print [, followed by a character representing the type of the array's elements (in your case C for char), followed by @ then the "identity hash code" of the array (think of it like you would a "memory address").

This sad state of affairs is generally considered as a "mistake" with java.

See this answer for a list of other "mistakes".

System.out.print(Arrays.toString()); is not working for array of OBJECTS

This part of your code...

    }
public String toString(){

}

closes the Circle class before it includes your toString() method. Therefore, you should rewrite it like...

    public String toString(){

}
}

and then just fill in whatever you want in the toString() method. Maybe something like...

return "Circle of radius " + radius; 

You may find that these issues are more easily detected if your actively organize your code for readability. I've cleaned the code you posted for reference...

package Shift;

import java.util.*;

public class Shift {

public static void main(String[] args) {

Shift c = new Shift();

Circle c1 = c.new Circle(4,6,4);
Circle c2 = c.new Circle(4,5,4);
Circle c3 = c.new Circle(5,4,4);
Circle c4 = c.new Circle(5,4,3);

Circle[] a = {c1, c2, c3, c4};

Arrays.sort(a);
System.out.print(a[0]);
}

public class Point{

private int x;
private int y;

public Point(int x, int y){
this.x = x; this.y = y;
}

public int getX(){
return this.x;
}

public int getY(){
return this.y;
}
}

public class Circle extends Point implements Comparable<Circle>{

private double radius;
private Point point;

public Circle(int x, int y, double radius) {
super(x, y);
this.radius = radius;
}

public double getRadius(){
return this.radius;
}

public Point getPoint(){
return this.point;
}

public int area(){
return (int) (Math.PI*radius*radius);
}

public int compareTo(Circle other){
if(this.area()>other.area())
return 1;

if(this.area()<other.area())
return -1;
else if(this.getX()>other.getX())
return 1;

if (this.getX()<other.getX())
return -1;
else if(this.getY()<other.getY())
return -1;
else
return 1;
}

@Override
public String toString(){
return "Circle of radius " + radius;
}
}

}

Arrays.toString, no library

Then I will create my own toString like this :

public String toString(int[] arr) {
StringBuilder result = new StringBuilder();
result.append("[");
for (int a : arr) {
result.append(a);
}
result.append("]");
return result.toString();
}

java printing an array with Arrays.toString() error

method toString in class Object cannot be applied to given types. required: no arguments found: int[] reason: actual and formal argument list differ in length

May be you have a variable named Arrays, that's why the compiler is complaining thinking that you are trying to invoke the Object.toString(), which doesn't take any argument.

Try

 System.out.println(java.util.Arrays.toString(totals)); 

java.util.Arrays.toString method is not overloaded for Strings in Scala

It is actually a bug in java that it works. If you look at the list of candidates, you'll see, that clearly there is no suitable alternative. It ends up calling Array[Object] variant by accident, but that is wrong, becuase Array is invariant in its type parameter (all generic types are invariant in java), so Array[Object] is not a superclass of Array[String].

Why toCharArray() and toString() method doesn't work

toString() called on a char array doesn't do the same.

When you write

String t3 = t.toString();

you said that you want to call toString() method from Object.class.
Because char array cannot have an overrided version of toString() that will do what you re expected, because it's an array of primitive type.
It works as expected, it returns you the string representation of a link to that particular char array in a heap.

Arrays.toString(t) returns you the string representation of an initial array.
In your case it will return "[a, b, c]".
Then when you're trying to call ar.contains(t2) you re trying to find "[a, b, c]" string in your initial array which doesn't have it. It have 4 strings inside.
"abc", "def", "ghi", "jkl".
It doesn't contain the requested element ("[a, b, c]" string).

I would recommend you to debug your program. You will see which values are actually in your variables. It will give you a lot more understanding about this situation.

Errors in toString method and Recursion method that returns sum of array

Your toString() method should look like this:

public String toString() {
return Arrays.toString(numArray);
}

You were missing the return statement and your call to Arrays.toString() had an additional and wrong argument.

Calculating a sum with recursion is pretty pointless, but here is a solution:
https://stackoverflow.com/a/20253717/8883654

Java toString method in char[]

You want to use Arrays.toString(char[]{'a','b'});


You can use

char data[] = {'a', 'b', 'c'};
String str = new String(data);

See the javadoc

public String(char[] value)
Allocates a new String so that it
represents the sequence of characters currently contained in the
character array argument. The contents of the character array are
copied; subsequent modification of the character array does not affect
the newly created string. Parameters: value - The initial value of the
string

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Calling toString on an array will call the toString method from Object. Which will return you the hashCode

public String toString() Returns a string representation of the
object. In general, the toString method returns a string that
"textually represents" this object. The result should be a concise but
informative representation that is easy for a person to read. It is
recommended that all subclasses override this method. The toString
method for class Object returns a string consisting of the name of the
class of which the object is an instance, the at-sign character `@',
and the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Returns: a string representation of the object.



Related Topics



Leave a reply



Submit