-1

I need help with formatting a string that gets passed in from the command line argument. So when I type

java Main "Tassimo T46 Home Brewing System|43-0439-6|17999|0.30:Moto Precise Fit Rear Wiper Blade|0210919|799|0.0: Easton Stealth Reflex Composite Hockey Stick| 83-4567-0|8999|0.5:Yardworks 4-Ton Log Splitter|60-3823-0|39999|0"

So it should be formatted displaying the name followed by several spaces then the integers followed by another few spaces then the second part of the integers then spaces followed by the decimal.

example

coffee      123456     199999     0.30

using String.format`.

4
  • are you replacing the | with spaces? Commented Jan 27, 2014 at 5:04
  • 3
    and isn't this question somehow related to stackoverflow.com/questions/21372450/… ? Commented Jan 27, 2014 at 5:05
  • Try this one Use replaceAll("|", " "); Commented Jan 27, 2014 at 5:05
  • 1
    What's the logic that links the sample input to the sample output? The string "coffee" and most of those integer sequences don't appear in the input. Do you want a separate line for each product? Commented Jan 27, 2014 at 5:14

1 Answer 1

0

From here, you only can do things like this using String.format:

String.format("%1$s %2$s %2$s %3$s", "a", "b", "c");

and the output:

a b b c

So I would recommend @praveen_mohan's answer. Use replaceAll method, or iterate over your_input.split("|") and build the output using StringBuilder.

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

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.