how to split a string in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • praver235
    New Member
    • Feb 2013
    • 5

    how to split a string in java

    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.
    Code:
     String[] parts = output.split(" ");
            String[][] table = new String[parts.length / 2][2];
            for (int i = 0, r = 0; r < table.length; r++) {
            table[r][0] = parts[i++];
            table[r][1] = parts[i++];
    }
     System.out.println(java.util.Arrays.deepToString(table));
        }
    Last edited by praver235; Feb 16 '13, 07:34 PM. Reason: improved formatting
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • PreethiGowri
      New Member
      • Oct 2012
      • 126

      #3
      check for Split function, i hope it helps you

      Comment

      Working...