user3210880
user3210880

Reputation: 29

Java splitting command line argument

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"

Upvotes: 1

Views: 1181

Answers (4)

cafebabe1991
cafebabe1991

Reputation: 5176

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

Upvotes: 0

rendon
rendon

Reputation: 2363

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"

Upvotes: 1

Ivey
Ivey

Reputation: 499

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.

Upvotes: 0

aliteralmind
aliteralmind

Reputation: 20163

/**
   <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

Upvotes: 1

Related Questions