0

How could one populate a 2d array from a text file using split?

String proxies[][] = {{"127.0.0.1","80"}, {"127.0.0.1","443"}, {"127.0.0.1","3306"}};

In my text file I have data with a ip:port on each line:

127.0.0.1:80
127.0.0.1.443
127.0.0.1.3306

I could populate a 1d array using split like this:

proxies = everyLine.split("\\n");

How would I insert the ip:port data into a 2d array?

3 Answers 3

1
    String[] lines = everyLine.split("\\n");
    String[][] proxies = new String[lines.length][];
    int i=0;
    for ( String line : lines )
    {
        proxies[i++] = line.split(":");
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Using Java constructs it's not possible. You can use Apache Commons method FileUtils#lineIterator(File, String) to iterate over lines and apply String.split(String) on each

Comments

0

you could split on the : operator.

String []proxies = everyLine.split("\\n");
for(int i=0;i<proxies.length;i++){
 String[] anotherDimention= proxies[i].split(":");
// do something useful with it
}

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.