Angmar
Angmar

Reputation: 195

Java: Why does my toString method print wrong information?

I have an abstract superclass that has two attributes: int and string. I have overridden the toString method in it as well as in its subclass that has one extra attribute (LocalDate). However, for some reason that I don't understand, when I print the subclass toSring info, the int value changes.

This is what I have in the superclass:

public abstract class File {
private int id;
private String text;

public File(int newId, String newText) throws IllegalArgumentException {
      id(newId);
      text(newText);
}

public int id() {
   return id;
}

public void id(int e) throws IllegalArgumentException {      
   if (e <= 0) {
      throw new IllegalArgumentException();
   }
   else {
      id = e;
   }
}

public String text() {
   return text;
}

public void text(String aText) throws IllegalArgumentException {
   if (aText == null || aText.length() == 0) {
      throw new IllegalArgumentException();
   }
   else {
      text = aText;
   }
}

@Override
public String toString() {
   return '"' + id() + " - " + text() + '"';
}

Then in the subclass I have this:

public class DatedFile extends File {
private LocalDate date;

public DatedFile (int newId, LocalDate newDate, String newText) throws IllegalArgumentException {
   super(newId, newText);
   date(newDate);
}

public LocalDate date() {
   return date;
}

public void date(LocalDate aDate) throws IllegalArgumentException {
   if (aDate == null) {
      throw new IllegalArgumentException();
   }
   else {
      date = aDate;
   }
}
@Override
public String toString() {
   return '"' + id() + " - " + date + " - " + text() + '"';
}

I tested it like this:

public static void main(String[] args) {
   LocalDate when = LocalDate.of(2020, 1, 1);
   DatedFile datedFile1 = new DatedFile(999, when, "Insert text here");
   System.out.println(datedFile1);

It printed: "1033 - 2020-01-01 - Insert text here" However, if i use the following code

System.out.println(datedFile1.id());

it prints the correct id (999). So I assume that something with the toString messes it up, but I have no idea where the problem is.

PS. I'm a beginner and I'm sorry if I included too much code but as I don't know what is the problem, I really don't know what is relevant and what isn't.

Upvotes: 3

Views: 328

Answers (3)

SteinTech
SteinTech

Reputation: 4068

or using string interpolation:

return '\" ${id()} - ${date} - ${text()} \"';

Upvotes: -1

davechuks
davechuks

Reputation: 11

Change your toString() method from '"' to " \"".

'"' is a character (which is stored internally as an Integer), so adding it with id() produces the result you're seeing.

Upvotes: 1

Johannes Kuhn
Johannes Kuhn

Reputation: 15163

Your problem is here:

return '"' + id() + " - " + date + " - " + text() + '"';

id() returns an int, and '"' is a char, which is a numeric type. So '"' + 999 is 1033, not "999.

To fix the problem, use a String instead of a character:

return "\"" + id() + " - " + date + " - " + text() + "\"";

Upvotes: 11

Related Questions