I am using a toString method to display data in an arraylist. How do I write the code so that it displays the information without the [ , , ] around my information
see the code below:
case 6:
System.out.println("Enter an account number to view the transactions of the account");
number=keyboard.nextLong();
found=false;
try{
for(int i=0;i
if(aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
found=true;
System.out.println("Account " + number + ":\tStart Balance: " +money.format(aBank.getAccounts().get(i).getStartBalance()));
System.out.println(aBank.getAccounts().get(i).getTransaction().toString());
System.out.println("Ending Balance :" +money.format(aBank.getAccounts().get(i).getBalance()));
}
}
}
catch (Exception e){
System.out.println("Unable to process request.\n" +e.getMessage());
}
break;
this is the output:
Account 1: Start Balance: $1000.00
[Deposit 1 6/6/2011 $500.00
, Deposit 2 6/6/2011 $489.00
, Deposit 3 6/6/2011 $262.00
, Withdrawal 4 6/6/2011 $897.00
, Withdrawal 5 6/6/2011 $56.32
, Withdrawal 6 6/6/2011 $78.24
]
Ending Balance :$1219.44
notice the brackets and commas that need to be removed
解决方案
Don't use toString() at all. It is not meant to be used to create formatted UI strings. It is a developer tool, not a user presentation tool.
I will add a caveat for the cases where the object represents something with a simple accepted format that can serve both purposes. Java primitive wrappers (like Integer and Float) are an example of this. Other simple classes such as a 2D point can work in this regard as well, but this quickly falls apart as an object becomes more complex.
To do what you are attempting, just create a method to do your display and do the formatting in that method instead.