|
-
Jul 6th, 2007, 09:52 AM
#1
Thread Starter
Member
Last edited by DonCash; Jul 11th, 2007 at 06:26 AM.
Reason: Resolved that mother!
-
Jul 6th, 2007, 05:22 PM
#2
Hyperactive Member
Re: Split String into Array
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.
-
Jul 6th, 2007, 06:04 PM
#3
Frenzied Member
Re: Split String into Array
Why not use charAt() and treat the String as an array?
Code:
String st = "thisisatest";
for (int index=0; index<st.length(); ++index)
{
System.out.println(st.charAt(index));
}
-
Jul 8th, 2007, 09:53 PM
#4
PowerPoster
Re: Split String into Array
If you want to store them in a separate arrays, as System_Error says detect the , sign to split them.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jul 9th, 2007, 01:02 AM
#5
Re: Split String into Array
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
Last edited by ComputerJy; Jul 9th, 2007 at 03:51 AM.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 9th, 2007, 02:36 AM
#6
PowerPoster
Re: Split String into Array
Why? Until found the , sign store each of the previous element in an array.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jul 9th, 2007, 08:53 AM
#7
Thread Starter
Member
Re: Split String into Array
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.
Code:
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);
}
}
}
Last edited by DonCash; Jul 9th, 2007 at 09:38 AM.
-
Jul 9th, 2007, 09:27 PM
#8
PowerPoster
Re: Split String into Array
 Originally Posted by DonCash
Code:
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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jul 9th, 2007, 10:08 PM
#9
Re: Split String into Array
 Originally Posted by eranga262154
Think that can this code hold a string? I think better to store them an array.
Code:
String[] split = encodedPasswd.split(" ");
They are stored in an array
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 9th, 2007, 10:13 PM
#10
PowerPoster
Re: Split String into Array
Then what happened on that code segment I mentioned on my previous post?
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jul 9th, 2007, 10:21 PM
#11
Re: Split String into Array
 Originally Posted by eranga262154
Then what happened on that code segment I mentioned on my previous post?
An iteration through the array
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 10th, 2007, 12:54 AM
#12
PowerPoster
Re: Split String into Array
Ya, it is correct. Iteration through the array. But it is done by a letter?
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Jul 10th, 2007, 04:34 AM
#13
Thread Starter
Member
Re: Split String into Array
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.
-
Jul 10th, 2007, 04:51 AM
#14
Thread Starter
Member
Re: Split String into Array
Just figured it out,
It seems to work fine when I use this instead:
Code:
if (passBit.equals( "00f1" )) {
System.out.println("A");
}
else {
System.out.println("B");
}
-
Jul 10th, 2007, 06:18 AM
#15
Thread Starter
Member
Re: Split String into Array
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

------------------------------------------------------------
After much playing around and forum hunting i've solved the problem.
I've used this code:
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
Last edited by DonCash; Jul 11th, 2007 at 06:25 AM.
Reason: Sorted!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|