Suena un poco estúpido, pero necesito ayuda con mi método toString() y es muy molesto. Intenté buscar en línea porque toString es el que está jodiendo y "no encuentra al constructor Kid # 2" aunque esté allí e incluso haría algo más y no funciona. Ok que era mucho, así que aquí es mi código:¿Cómo sobrescribir toString() correctamente en Java?
import java.util.*;
class Kid {
String name;
double height;
GregorianCalendar bDay;
public Kid() {
this.name = "HEAD";
this.height = 1;
this.bDay = new GregorianCalendar(1111,1,1);
}
public Kid (String n, double h, String date) {
// method that toString() can't find somehow
StringTokenizer st = new StringTokenizer(date, "/", true);
n = this.name;
h = this.height;
}
public String toString() {
return Kid(this.name, this.height, this.bDay);
}
} //end class
Ok Así que mi toString anterior (lo sé, mi tercer parámetro está desactivado, debe ser una cadena) está apagado. Si codigo un valor para el tercero, se vuelve loco y dice que no puede encontrarlo (arriba). Entonces, ¿cómo puedo obtener la fecha y romperla?
Clase llamar a este se encuentra por debajo
class Driver {
public static void main (String[] args) {
Kid kid1 = new Kid("Lexie", 2.6, "11/5/2009");
System.out.println(kid1.toString());
} //end main method
} //end class
intenté investigar varios constructores y realmente no ayudó. Intenté investigar los métodos de String() y traté de usar la lógica anterior de los métodos toString() que creé anteriormente, pero esto es completamente nuevo, así que nunca funcionó.
¿Ayuda?
Por favor, publique el código que realmente compilaría. Su método 'toString()' no devuelve una cadena, y no puede llamar a un constructor así. No está claro lo que estás tratando de lograr. Por favor, lea http://tinyurl.com/so-hints y aclare su pregunta. –
The Spring's ToStringCreator (https://github.com/SpringSource/spring-framework/tree/master/spring-core/src/main/java/org/springframework/core/style) hace el trabajo muy bien. – Ritesh