By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

how to split a string in java

P: 5
The string is
[Ian Wood P. M. Visscher]
[Ian Wood L. Mengersen]
[Ian Wood]
[L. Mengersen Ian Wood]

Ian A. Wood is name of first person followed by a space and then name of second person P. M. Visscher. Similarly for the next string.I want to split the string person wise and store it in an array.For example a[0][0]=Ian Wood , a[0][1]=P. M. Visscher ,a[1][0]=Ian Wood ,a[1][1]=L. Mengersen and so on..Every time when I would give input, the names in the string will change.
How do I split it and store it in the above form in the array.What should I do to get the preferred output?Thank you.

Following is the code I worked on,but does not split properly.
Expand|Select|Wrap|Line Numbers
  1.  String[] parts = output.split(" ");
  2.         String[][] table = new String[parts.length / 2][2];
  3.         for (int i = 0, r = 0; r < table.length; r++) {
  4.         table[r][0] = parts[i++];
  5.         table[r][1] = parts[i++];
  6. }
  7.  System.out.println(java.util.Arrays.deepToString(table));
  8.     }
Feb 16 '13 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 10K+
P: 12,421
You have to first tell us how you define a person's name. For example, if the input is John Smith Peter Jackson Williams, is it John Smith and Peter Jackson Willaims? Or is it John Smith Peter and Jackson Williams.

Unless you have clearly defined rules on how to group the names, you can't even begin to define a solution.
Feb 16 '13 #2

P: 126
check for Split function, i hope it helps you
Feb 25 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.