1

I have an array with 2 data fields in each element:

String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};

The first and last names and separated by a tab ("\t"). How would I go about splitting them into two arrays, for example:

String[] firstName = {"Bob", "Barbara", "John"};
String[] lastName = {"Marley", "Newton", "Smith"};

I initially tried split("\t") but that didn't work, and I've tried looking up for similar questions here to no avail.

One thing to note, I am not using ArrayList.

Any help would be immensely appreciated. Thank you in advance.

Code snippet:

public static String[] sortNames(String[] info) {

    String[] firstName = new String[info.length];
    String[] lastName = new String[info.length];

    for(int i = 0; i < info.length; i++) {
        firstName[i] = info[i].split("\t");
    }

    return firstName;
}
6
  • Can you show your attempt and clarify what you mean by didn't work Commented Jul 23, 2018 at 3:43
  • Try .split("\\s+") Commented Jul 23, 2018 at 3:47
  • see this stackoverflow.com/questions/3762347/… Commented Jul 23, 2018 at 3:47
  • Edited post to show snippet Commented Jul 23, 2018 at 3:48
  • split returns an array and hence this firstName[i] =.. won't compile Commented Jul 23, 2018 at 3:50

4 Answers 4

2

firstName[i] = info[i].split("\t"); is assign an array to an element,it will cause compile failure.

public static String[] sortNames(String[] info) {

    String[] firstName = new String[info.length];
    String[] lastName = new String[info.length];

    for(int i = 0; i < info.length; i++) {
        String[] infos = info[i].split("\t");
        firstName[i] = infos[0];
        lastName[i] = infos[1];
    }

    return firstName;//also,you might need to change your return type to String[][] so that both array can be returned
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could have your sortNames method return a two-dimensional array:

import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};
    String[][] names = sortNames(myArr);
    String[] firstNames = names[0];
    String[] lastNames = names[1];
    System.out.println("names: " + Arrays.deepToString(names));
    System.out.println("firstNames: " + Arrays.toString(firstNames));
    System.out.println("lastNames: " + Arrays.toString(lastNames));
  }

  public static String[][] sortNames(String[] info) {
    int infoArrLength = info.length;
    String[][] names = new String[2][infoArrLength];
    for(int i = 0; i < infoArrLength; i++) {
      String[] infos = info[i].split("\\s+");
      names[0][i] = infos[0];
      names[1][i] = infos[1];
    }
    return names;
  } 
}

Output:

names: [[Bob, Barbara, John], [Marley, Newton, Smith]]
firstNames: [Bob, Barbara, John]
lastNames: [Marley, Newton, Smith]

Comments

0
String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};
    String[] firstName = new String[myArr.length];
    String[] lastName = new String[myArr.length];
    for (int i = 0; i < myArr.length; i++) {
        String[] splitString = myArr[i].split("\\s+");
        firstName[i] = splitString[0];
        lastName[i] = splitString[1];
    }

Comments

0

You can simply use java regx to perform the splitting.

\s => A whitespace character, short for [ \t\n\x0b\r\f]

private static void splitArray(String[] arr) {
    int len = arr.length;
    String[] firstNames = new String[len];
    String[] lastNames = new String[len];
    for (int i = 0; i < len; ++i) {
        String[] names = arr[i].split("\\s+");
        firstNames[i] = names[0];
        lastNames[i] = names[1];
    }
    System.out.println(Arrays.deepToString(firstNames));
    System.out.println(Arrays.deepToString(lastNames));
}

If I were you, in your case, I'd prefer using javafx.util.Pair to bind the first and last name together as follows and in java 8 using stream will make it cleaner:

private static void splitArrayUsingStream(String[] arr) {
    Pair[] pairs = Arrays.stream(arr).map(s -> {
        String[] names = s.split("\\s+");
        return new Pair<>(names[0], names[1]);
    }).collect(Collectors.toList()).toArray(new Pair[arr.length]);
    System.out.println(Arrays.deepToString(pairs));
}

These methods will give us output as:

[Bob, Barbara, John]
[Marley, Newton, Smith]
[Bob=Marley, Barbara=Newton, John=Smith]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.