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..>
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(":");
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...
> [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 >

>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 >

> >
>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]);
}
>
> 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 >
