1

I am having trouble splitting a string when a character is found. I know how to split strings when it is in an array. But I don't know how to split a string when it is passed as a command line argument. This is a string argument that gets passed in and I have to add spaces when the bitwise Or is found and also when the colon is found I have to add a new line. I don't really know how to approach this problem when it gets passed as a argument. Any help would be awesome thanks.

"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"
1
  • 1
    The command line arguments are in an array. Please clarify your question, and post some code that you have tried so far Commented Jan 27, 2014 at 3:30

4 Answers 4

1
/**
   <P>{@code java SplitXmpl}</P>
 **/
public class SplitXmpl  {
   public static final void main(String[] igno_red)  {
      String sInput = "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";

      String sOutput = sInput.replaceAll("\\|", " ").replaceAll(":", System.getProperty("line.separator", "\n"));

      System.out.println(sOutput);
   }
}

Output:

[C:\java_code]java SplitXmpl
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
Sign up to request clarification or add additional context in comments.

Comments

1

Another possible solution:

public static void main(String[] args) {
    String s = args[0];
    s = s.replace("|", " ").replace(":", "\n");
    System.out.println(s);
}

Run with:

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"

Comments

0

Command line arguments is nothing but a String array. So you can work on them just like any other String object. Have a look at the String API for the functionality you are trying to implement, the replace() method should suffice for you.

Comments

0

The above answers are correct if the following assumptions are used

  1. The input is a hard code string or
  2. The input when given as a command line argument should have no space for then it will be treated as different argument.for eg :asasa asaas assaa has three argument and asasaasaasassaa has only 1 argument

for the first case the above answers can work but for the second case the following code snippet works

INPUT

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

public class test {
    public static void main(String[] args) {
        int l=args.length;
        StringBuilder builder=new StringBuilder();
        while(l-->0){
            builder.append(args[l]);
        }

        System.out.println(builder.toString().replace("|", " ").replace(":","\n"));
    }
}

OUTPUT

Splitter 60-3823-0 39999 0Log4-Ton83-4567-0 8999 0.5 YardworksStick HockeyCompositeReflexStealthEastonBlade 0210919 799 0.0 WiperRearFitPreciseSystem 43-0439-6 17999 0.30 MotoBrewingHomeT46Tassimo

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.