Reputation: 2454
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<aBank.getAccounts().size();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
Upvotes: 1
Views: 10706
Reputation: 24262
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.
Upvotes: 3
Reputation: 68
Do you really need to use the toString() Method? Otherwise you could build up the string for each array by yourself.
tmpArray = aBank.getAccounts().get(i).getTransaction();
StringBuilder builder = new StringBuilder();
for (String value : tmpArray) {
builder.append(value);
}
System.out.println(builder.toString());
Upvotes: 2
Reputation: 1595
Try to write it more readable.
try {
for(BankAccount ba : aBank.getAccounts()) {
if(ba.getAccountNumber().equals(number)) {
found=true;
System.out.println("Account " + number + ":\tStart Balance: "
+money.format(ba.getStartBalance()));
for (Object tr : ba.getTransaction()) {
System.out.println(tr);
}
System.out.println("Ending Balance :" +money.format(ba.getBalance()));
}
}
}
Upvotes: 1
Reputation: 120496
Use a guava joiner as in
Joiner.on("").join(
aBank.getAccounts().get(i).getTransaction())
Upvotes: 2
Reputation: 424993
What about this?
List<String> list = ArrayList<String>();
String asString = list.toString().replaceAll("^\\[", "").replaceAll("\\]$", "").replace(",", "");
Upvotes: 1
Reputation: 14769
Modify the "toString" method of the Object returned by "getTransaction()" !?
Upvotes: 1