-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`.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • are you replacing the | with spaces? – Leo Jan 27 '14 at 05:04
  • 3
    and isn't this question somehow related to http://stackoverflow.com/questions/21372450/java-splitting-command-line-argument ? – Leo Jan 27 '14 at 05:05
  • Try this one Use replaceAll("|", " "); – praveen_mohan Jan 27 '14 at 05: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? – Ted Hopp Jan 27 '14 at 05:14

1 Answers1

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.

Community
  • 1
  • 1
Miguel Jiménez
  • 1,276
  • 1
  • 16
  • 24