-9

I am splitting a string with a comma as a delimeter using String.split() function in java.

Input

1,1,87 gandhi road,600005

Output:

1
1
87

The code stops at whitespace. How do I get it to work ?

Karup
  • 2,024
  • 3
  • 22
  • 48
Sidhanth Sur
  • 303
  • 2
  • 9
  • How about taking a look at the documentation first? http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) and also doing a few google searches? http://stackoverflow.com/questions/10631715/splitting-a-comma-separated-string And also looking at the tour page? http://stackoverflow.com/tour – xrisk Jun 27 '15 at 11:40

3 Answers3

2

We'll need to see your code before we can begin troubleshooting. However, the following code should work just fine:

String address = "1,1,87 gandhi road,600005";

String[] stringArray = address.split(",");

for(String str : stringArray)
{
    // Do something with str.
}
Tom
  • 16,842
  • 17
  • 45
  • 54
SE13013
  • 343
  • 3
  • 14
1

This works for me:

String original = "1,1,87 gandhi road,600005";
String[] s = original.split(",");
for (String t : s) {
    System.out.println(t);
}

Output:

1
1
87 gandhi road
600005

Tom
  • 16,842
  • 17
  • 45
  • 54
TDG
  • 5,909
  • 3
  • 30
  • 51
1

Check the following code:

public class Main {
    public static void main(String[] args) {
        String[] str = "1,1,87 gandhi road,600005".split(",");
        for (String s : str) {
            System.out.println(s);
        }
    }
}

Output:

1
1
87 gandhi road
600005

Tom
  • 16,842
  • 17
  • 45
  • 54
KDP
  • 1,481
  • 7
  • 13
  • Please mind, that I've changed your `print` call to a `println` call so your code produces the output you've given. – Tom Jun 27 '15 at 11:55