Splitting an string of number into multiple lines
I got one string test="101010101010101010101010101010101010101010" it's 20x10 and want to split it into one output that shows 5x10's in a line.
Been thinking on using .charAt() in one for loop but having troubble twisting my ideas on getting it done.
Been doing java tasks all easter, since the weather have sucked too mutch.
Re: Splitting an string of number into multiple lines
For subdividing a String at specific lengths, use substring(...).
db
Re: Splitting an string of number into multiple lines
here is what i've gotten into so far, one eased down code cut.
Code:
class stringedit{
public static void main(string[]args){
String testString = "20 21 22 23 24 25 26 27 28 29";
String finalString=""; // MAX 5 NUMBERS EACH LINE
int testlength = testString.length();
for (int i=0;i<testlength;i++){
finalString = finalString+testString.substring(15)+"\n"
}// END FOR
}// END MAIN
}//END STRINGEDIT
the whole program was to enter 20 numbers in between 20 and 50. store them in one array and print them out in one messagebox with only 5 numbers each line.
having some troubble on the FOR loop.
also, are my thoughts so far going the right direction ?
Re: Splitting an string of number into multiple lines
Where's your array? Will the user be entering numbers separated by spaces? If so, look into String#split(...) to create the String array for you.
Re: Splitting an string of number into multiple lines
that was just one example code. had to make it so i could translate into English also.
I've entered the numbers 20 times seperatly and it's stored in one array.
and then pulled out of the array and into one string with one " " in between each number so they won't be clustered.
also made one check that if the number typed in ain't in between 20 and 50 you will get one message and type it in again.
also trying not to be rude to get people to code for me but sending me in the right direction and maybe one concept code for the current problem at hand.
Re: Splitting an string of number into multiple lines
Code:
class stringedit{
public static void main(string[]args){
String testString = "20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39";
String finalString=""; // MAX 5 NUMBERS EACH LINE
int testlength = testString.length();
for (int i=0;i<testlength;i++){
finalString = finalString+testString.substring(i,i+14)+"\n"
i+=14;
}// END FOR
}// END MAIN
}//END STRINGEDIT
and then problem is solved, had to use another forum for some help. :(giggle): might be the good part of internet. =)