0

I am trying to use toString method and return a properly formatted string:

public String toString()
{
  return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}

Should format with tabs between each variable, but for the first line, the second tab doesn't seem to apply... (fake names used in the code) This is what is printed:

72354   Ted Murphy$132.90
69713   Anita Gomez     $111.52
93757   Sanchit Reddy   $785.90
mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

2

It's all because the strings are of the different lengths, but tabulation idents the text to the next tab position. For such a formatting it is better to use, something like:

System.out.format("%32s%32s%16s", acctNumber, name, fmt.format(balance));

This will format your output as 32 chars for first and second variables and 16 for the third. You may vary this lengths, to make a formatting as you need. And you may need to specify data type if some of the variables are not of the String type.

You can read about it here

Stanislav
  • 27,441
  • 9
  • 87
  • 82