0

I have a method in my switch statement explaining to print my arraylist (i.e - System.out.println(drinkList);)

           ArrayList<String> drinkList = new ArrayList<String>();
           System.out.print("Please enter a drink information to add:\n");
           inputInfo = scan.nextLine().trim();
           drinkLink = DrinkParser.parseStringToDrink(inputInfo);
           drinkList.add(drinkLink.toString()); //Take in user data to parse into parts

Then I called it using the code System.out.println(drinkList);

My problem is the output prints the following as such:

[

Data Entry 1

,

Data Entry 2

]

I want to remove the brackets and the comma.

2
  • 1
    Show us what your method contains then Commented Feb 4, 2014 at 22:48
  • I'll add basic code to my OP. It's a lot of code. Commented Feb 4, 2014 at 22:49

2 Answers 2

1

Don't call the toString() method on the ArrayList but loop through it and build a string yourself. Do something like:

StringBuilder builder = new StringBuilder();
for (String value : drinkList) {
    builder.append(value) + ",";
}
String text = builder.toString();
System.out.println(text);

That'll make sure that the resulting string has the format that you want - in this case comma-separated entries.

Sign up to request clarification or add additional context in comments.

1 Comment

The builder.append is giving me an error, not letting me compile.
0

Use the following code to remove the brackets and the commas:

String s = "[\nData Entry 1\n,\n Data Entry 2\n]";
String result = s.replaceAll("[^\\dA-Za-z\n]", "");
System.out.println(result);

The result is:

Data Entry 1

Data Entry 2

Or, you can override toString() method for your class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.