-1

I'm having main method arguments separated by commas and some of the values might have white spaces also, I have to split only comma separated values ignoring whitespaces.

eg: abcdef,xyzert,mnop qrst

I have tried using split(",") but it's giving me 4 values instead of 3 as the last element should be treated as one element.

Here's my code:

public class ArgCheck {
    public static void main(String [] args) {
        String x = Arrays.toString(args);
        System.out.println(x);
        String [] y = x.split(",");
        System.out.println(y.length);
    }
}

Please help

Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
Kranthi Sama
  • 498
  • 3
  • 10
  • 28
  • 2
    I can't reproduce this locally using `"abcdef,xyzert,mnop qrst".split(",")`. Can you tell us more about your setup? – Tim Biegeleisen Jul 22 '15 at 05:10
  • Show us your exact code.. Can't reproduce this – TheLostMind Jul 22 '15 at 05:10
  • @KranthiSama : Do you need the `mnop qrst` as a single parameter? – mustangDC Jul 22 '15 at 05:19
  • Java can do nothing for you here; what you need to do is quote arguments correctly in your command interpreter. – fge Jul 22 '15 at 05:32
  • @KranthiSama : Did you try my answer? – mustangDC Jul 22 '15 at 05:36
  • If you are passing `abcdef,xyzert,mnop qrst` as your `args` to your main methods then you can check the values and length of `args`. Length will be 2 and values will be `abcdef,xyzert,mnop` & `qrst`. – Naman Gala Jul 22 '15 at 05:49
  • From the java docs for the `Arrays.toString` method: *Adjacent elements are separated by the characters ", " (a comma followed by a space)* Thus, a comma is added between "mnop" and "qrst", producing 4 results. To solve this, find an alternative to `Arrays.toString()` for argument concatenation or change input to `abcdef,xyzert,"mnop qrst"` – Ian2thedv Jul 22 '15 at 06:08

4 Answers4

1

If you are passing abcdef,xyzert,mnop qrst as your args to your main methods then you can check the values and length of args. Length will be 2 and values will be abcdef,xyzert,mnop & qrst.


To achieve what you want you can change your input in command line to abcdef,xyzert,"mnop qrst". You need to use "" to identify it as one word.

And if you don't know what input will be as in which field will have space in between then you can use general rule

to wrap all your words in "" and pass it to main method like

"abcdef","xyzert","mnop qrst"

or wrap entire line in "" and then pass it to main method like

"abcdef,xyzert,mnop qrst"

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

Try this once, receive the variable string as your command-line argument.

String string = "abcdef,xyzert,mnop qrst";
String[] parts = string.split(",");
String part1 = parts[0]; 
String part2 = parts[1]; 
String part3 = parts[2];
System.out.println(part1+"\n"+part2+"\n"+part3);

If you need n number of values in that case simply use a loop

for(int i=0;i<parts.length;i++)
    System.out.println(parts[i]);

Hope it helps

mustangDC
  • 945
  • 1
  • 12
  • 33
0

First try removing the white spaces from the String first.

st.replaceAll("\\s+","")

got this from Removing whitespace from strings in Java

After removing the white spaces, you may now use the split method.

Community
  • 1
  • 1
Matthew
  • 302
  • 1
  • 2
  • 12
  • I tried this code snippet String foo = "abcdef,xyzert,mnop qrst"; for(String s : foo.split(",")){ System.out.println(s); } It gave me 3 values instead of 4, [abcdef][xyzert][mnop qrst] respectively – Matthew Jul 22 '15 at 05:32
0

You can join all the elements of the args first and then split it. Like this:

String str = "";
for (String eachArg : args) {
    str += eachArg + " ";
}
System.out.println (str.split(",").length);

Hope this helps.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I dont want to loose white space. white space should not be ignored. @Sweeper – Kranthi Sama Jul 22 '15 at 05:32
  • Answer edited. Now you can just concatenate a " " to the string and you can also check whether it is the last argument and if it is then don't add the space. @Kranthi Sama – Sweeper Jul 22 '15 at 05:37
  • 2
    It is generally not optimal to concatenate Strings using `+` *inside a loop*. For each iteration a new `StringBuilder` is created, unnecessarily creating intermediate Strings. Better to use `StringBuilder` from the get go. See: http://stackoverflow.com/questions/7817951/string-concatenation-in-java-when-to-use-stringbuilder-and-concat and – Ian2thedv Jul 22 '15 at 06:17