I have this txt file containing names and scores. "ex. Player1 35".
My program able to read it and store it to an Array of String.
Here's my question.
Is possible to separate the name and the score and store them in different Arrays?

ex. myArray = {"Player1 35","Player2 30"};

i want to save "Player1 -" in a String Array.
and "35" to an Int Array. If it is, then kindly give me some ideas on how to do it.
Ideas or sample code is much appreciated thank you.

Recommended Answers

All 3 Replies

There are multiple ways of doing that. You can use the split function on a String, or use StringTokenizer

It's easy to split up a string into an array of strings, using (eg) a blank as a delimiter

String s = "abc 123";
String[] parts = s.split(" ");
// now   parts[0] = "abc", parts[1] = "123"

To convert the score from String to int you can use the Integer.parseInt method.

Thank you for the reply.
Got it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.