PDA

Click to See Complete Forum and Search --> : [RESOLVED] Split String into Array


DonCash
Jul 6th, 2007, 09:52 AM
Hey,

I've written this code to split the string defined in 'encodedPasswd'.

Any idea how I would get each segment into seperrate Arrays which can be called in another part of the code? :sick:


String encodedPasswd = "00f1,0021,0041,0076,0007,00a7,00c7,00f1";
String split[] = encodedPasswd.split(",");
for (int cnt = 0; cnt < split.length; cnt++)
{
System.out.println(split[cnt]);
}


Cheers for your help guys :thumb:

xxarmoxx
Jul 6th, 2007, 05:22 PM
What I would do is tokenize the input encodedPasswd. With that you know how many arrays you need, then you can dynamically create as many arrays as you need and put in the split text.

System_Error
Jul 6th, 2007, 06:04 PM
Why not use charAt() and treat the String as an array?


String st = "thisisatest";
for (int index=0; index<st.length(); ++index)
{
System.out.println(st.charAt(index));
}

eranga262154
Jul 8th, 2007, 09:53 PM
If you want to store them in a separate arrays, as System_Error says detect the , sign to split them.

ComputerJy
Jul 9th, 2007, 01:02 AM
Any idea how I would get each segment into seperrate Arrays which can be called in another part of the code
I think it's the String.toCharArray you are looking for

You can go for a 2D array of chars to store each part as an array

eranga262154
Jul 9th, 2007, 02:36 AM
Why? Until found the , sign store each of the previous element in an array.

DonCash
Jul 9th, 2007, 08:53 AM
Hey,

I have written this code as a test... Basically it splits the encodedPasswd string and returns only the first value 00f1.

I have included an if statement to say if the result is 00f1 then print 'A' else print 'B'.

For some reason, even though the value is 00f1, it returns B.

System.out.println(passBit); returns 00f1 so I don't understand?! Any ideas?

Please forgive me, I am new to Java.


public static void main(String[] args) {

String encodedPasswd = "00f1 0021 0041 0076 0007 00a7 00c7 00f1";
String[] split = encodedPasswd.split(" ");


for (int cnt0 = 0; cnt0 < split.length - 7; cnt0++) {

String passBit = (split[cnt0]);


if (passBit == "00f1") {
System.out.println("A");
} else if (passBit != "00f1") {
System.out.println("B");
}

System.out.println(passBit);



}

}
}

eranga262154
Jul 9th, 2007, 09:27 PM
for (int cnt0 = 0; cnt0 < split.length - 7; cnt0++) {

String passBit = (split[cnt0]);




Think that can this code hold a string? I think better to store them an array.

ComputerJy
Jul 9th, 2007, 10:08 PM
Think that can this code hold a string? I think better to store them an array.
String[] split = encodedPasswd.split(" ");They are stored in an array

eranga262154
Jul 9th, 2007, 10:13 PM
Then what happened on that code segment I mentioned on my previous post?

ComputerJy
Jul 9th, 2007, 10:21 PM
Then what happened on that code segment I mentioned on my previous post?
An iteration through the array :confused:

eranga262154
Jul 10th, 2007, 12:54 AM
Ya, it is correct. Iteration through the array. But it is done by a letter?

DonCash
Jul 10th, 2007, 04:34 AM
Any ideas why its not returning 'A' even though the string is '00f1' ?

String passBit = (split[cnt0]); Prints 00f1 but for some reason the code doesnt seem to think thats the output in the if statement.

DonCash
Jul 10th, 2007, 04:51 AM
Just figured it out,

It seems to work fine when I use this instead:


if (passBit.equals( "00f1" )) {
System.out.println("A");
}
else {
System.out.println("B");
}

DonCash
Jul 10th, 2007, 06:18 AM
I'm currently splitting the encodedpassword string using the , delimiter.

Do you guys know how to make this split after every 4 characters instead?

I need to turn:

00f1002100410076000700a700c700f1

into

00f1 0021 0041 0076 0007 00a7 00c7 00f1

:cool:

------------------------------------------------------------

After much playing around and forum hunting i've solved the problem.

I've used this code:


String encodedPasswd = "00f1002100410076000700a700c700f10";

assert encodedPasswd.length() % 4 == 0;
String[] splitLine = new String[encodedPasswd.length()/4];

for (int index = 0; index < splitLine.length; index++)
splitLine[index] = encodedPasswd.substring(index*4,index*4 + 4);

System.out.println(splitLine[0]);
System.out.println(splitLine[1]);
System.out.println(splitLine[2]);
System.out.println(splitLine[3]);
System.out.println(splitLine[4]);
System.out.println(splitLine[5]);
System.out.println(splitLine[6]);
System.out.println(splitLine[7]);
System.out.println(splitLine[8]);
System.out.println(splitLine[9]);



Lovely :bigyello: