tags:

views:

77

answers:

2

I have a text file with the following in it:

name1:0|0|0|0|0|
name2:0|0|0|0|0|
... etc

I'm importing the names into an array of Strings.

That's fine, however I can't think of a clean way to associate the numbers with the array item. The numbers are separated by a "pipe" '|' character

Ideally I'd like to call a method which returns an array of Integers when given the name i.e. something like public int[] getScores(String name)

+4  A: 

Use string split

First use : to split a line, then use | to split out each number as a string. At last use Integer.Parse to get the numbers.

Yin Zhu
I ended up using a combination of this and 2D arrays... thanks!
Redandwhite
Most likely, you'd be better off using a HashMap<String, int[]> than a 2D array. That should get you your 'ideal' method call that you describe above.
Kevin Day
+6  A: 

Scanner can also do it (since Java 1.5). The advantages over String#split is that you get some sort of automatic type conversion using regular expressions.

Example from javadoc

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close(); 

Also, if your aim is to recover the numbers by their name, use some kind of hash table to store it for faster retrieval.

Guido
Yes- a hashtable with the name as the key, and the array of ints as the values.
RichardOD