spliting data and putting in array..

hi... i have this data...

alex:class1 dayle:class2 zahara:class3 zaida:class4

I want to split it by space and by ":"

then take the left side and put it in one array and put the right side in another array. or put in a hashtable as key and value.....

i have this so far:

String[] splitvals= string1.split(" ");

splitvals=string1.split(":");

for (int i=0; i<splitvals.length; i++){

//System.out.println(splitvals[i]);

System.out.println(splitvals[i]);

}

then im lost from here.... any help much appreciated..>

[786 byte] By [h2opologirly69a] at [2007-11-27 9:34:54]
# 1

String[] splitvals= string1.split(" ");

splitvals=string1.split(":");

Assigning to a variable overwrites its previous value. Do you see that these two lines are equivalent to:

String[] splitvals=string1.split(":");

BigDaddyLoveHandlesa at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 2

I'd suggest the following algorithm:

Split the string on space, so that the component strings look line "alex:class1"

For each component string:

split it on colon, so its component strings look line "alex" and "class1"

then go from there with maps, arrays or whatever...

BigDaddyLoveHandlesa at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 3
ahh i seee... ill try that now.....
h2opologirly69a at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 4

String[] splitvals= string1.split(" ");

for (int i=0; i<splitvals.length; i++){

splitvals[i].split(":");

System.out.println(splitvals[i]);

}

like this?>

h2opologirly69a at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 5

> [code]

>

>String[] splitvals= string1.split(" ");

>for (int i=0; i<splitvals.length; i++){

>splitvals.split(":");

>System.out.println(splitvals);

>}

>

> code]

>

> like this?

Well, does it work?

jverda at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 6
no it doesn't....
h2opologirly69a at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 7
logically it doesnt work it looks like it should but it doesnt split it ...
h2opologirly69a at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 8
What exact results are you getting?
jverda at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 9
>splitvals[ i ].split(":");Strings are immutable. You can't change a String object's contents after creation. Therefore, with methods like split, replaceAll, toUpperCase, etc., a new String is created and returned. You're just throwing that new string away here.
jverda at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 10
alex:class1dayle:class2 zahara:class3 zaida:class4even though the split by : is there it doesnt do it...
h2opologirly69a at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 11

> >

>String[] splitvals= string1.split(" ");

>for (int i=0; i<splitvals.length; i++){

>splitvals[i].split(":");

>System.out.println(splitvals[i]);

>}

>

> code]

>

> like this?

i think it should b corrected this way

[code]

String[] splitvals= string1.split(" ");

for (int i=0; i<splitvals.length; i++){

string s[]= splitvals[i].split(":");

System.out.println(s[0] + " " + s[1]);

}

>

manishmulani@nitka at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 12
> alex:class1> dayle:class2> zahara:class3 > aida:class4> > > even though the split by : is there it doesnt do it...It does do it. You just throw away the results. See reply 9.
jverda at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 13
okay thank you i already have them in separate lists now .... quick question the arrays i created if i want to access them from somewhere else i have to make get methods for them correct. somewhere else as in another class... correct?
h2opologirly69a at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 14

> okay thank you i already have them in separate lists

> now .... quick question the arrays i created if i

> want to access them from somewhere else i have to

> make get methods for them correct. somewhere else as

> in another class... correct?

You'd have to either pass them as parameters, or store them as member variables and make them accessible through get methods.

jverda at 2007-7-12 23:00:29 > top of Java-index,Java Essentials,Java Programming...
# 15
yes
manishmulani@nitka at 2007-7-21 23:03:13 > top of Java-index,Java Essentials,Java Programming...