-6

Thanks for reading this. I'm currently making a phonebook project which required me to use only one array to store values. My question is how can you split a String inside of an array into two value? So I can search someone's name and get their name and number.

2
  • Is this homework?
    – bakki
    Jan 12, 2017 at 3:36
  • instead of saying something to OP, i would like to ask moderators to temporarily block the accounts of all those who has answered for easy rep... Jan 12, 2017 at 3:43

4 Answers 4

3

You can simply use the String#split method on any element of the array, whose delimiter can be any character. Here is an example where I chose : to be the delimiter.

String[] information = { "Castiel Li:123-456-7890" };

String[] args = information.split(":");

String name = args[0];
String phoneNumber = args[1];
3

If you have an array like the following:

String[] records = {"Bob, 1", "Mary, 2", "Castiel, 3"};

Then you can iterate over the record array in a loop and apply the String#split() method to each String object. In this case, the delimiter (character that separates tokes) in each string is the ',', so for a given String "name, number", you'd do:

String[] oneRecord = records[i].split(',');

Where oneRecord[i] would contain the two String objects "name" and "number"

3
  • So what would oneRecord stores? the first half of the string?
    – Castiel Li
    Jan 12, 2017 at 3:36
  • @CastielLi I'll edit with clarification. Also, would you clarify how your program is receiving the input, and whether your final array needs to be one-dimensional, specifically? You can use the logic I described to get the desired result, but it all depends on exactly what you wish to achieve. Jan 12, 2017 at 3:40
  • I think I got it. The final array will be one-dimensional and the program does receive input. I just wasn't sure how to split a string inside of an array. I think I will just use local variable to compare with the user input and the database
    – Castiel Li
    Jan 12, 2017 at 3:51
1

You haven't given sample inputs and outputs. But I think you are saying that you are storing the phone book in a single dimension array like following:

String[] array = {"Abc123", "xyz234", "pqr343"};

You can try something like following:

String phone1 = array[0].replaceAll("[^0-9]", "");
String name1 = array[0].replaceAll("[0-9]", "");

System.out.println(name1 + " " + phone1);
1

Something like this perhaps that use str.split()

import java.util.Arrays;

class Main {
  public static void main(String[] args) {

    String phoneBook[] = new String[2];
    phoneBook[0] = "John 1234567";
    phoneBook[1] = "Max 2345678";

    System.out.println(Arrays.toString(phoneBook));

    for(String contact : phoneBook)
       System.out.println("Name: " + contact.split(" ")[0] + " Phone Number: " + contact.split(" ")[1]);
  }
}

Output:

[John 1234567, Max 2345678]
Name: John Phone Number: 1234567
Name: Max Phone Number: 2345678

Try it here!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.