formatting text: StringTokenizer, array
I'm trying to make a program that takes a string and formats it so that it prints 3 words per line.
So for "hi hello how are you today" it would say:
hi hello how
are you today
Code:
import java.util.StringTokenizer;
public class six{
public static void main(String args[]){
String s="hi hello how are you today";
int numlines=3;
String [] format=new String[300];
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
for(int i=0;i<s.length();i++)
System.out.println(format[i]=st.nextToken());
Right now it just prints out 1 word per line w/ a NoSuchElementException error.
I know the StringTokenizer prints the 1 word per line...and at this point the array is just holding the tokens. Now I need to figure out how to set up the array to take 3 tokens then go to the next line....I'm stuck here.
Re: formatting text: StringTokenizer, array
Quote:
Originally Posted by
katiebear128
I know the StringTokenizer prints the 1 word per line...
No, StringTokenizer prints nothing. It returns one String per call, and you can use that for your advantage. To print a String without a new-line, use System.out.print. To do this 3 times, use a for loop. To avoid getting a NoSuchElementException, check that a token actually exists using one of the other methods of StringTokenizer, before trying to extract the token.
Incidentally, many here, and I think Java itself recommend moving away from using StringTokenizer and instead splitting the String.
Re: formatting text: StringTokenizer, array
Since it returns one string per call...I need 3 calls on one line then.
I'm not really sure how to set up that for loop. for(....) System.out.print(st.nextToken())....Right now my for loop doesnt even seem to have a purpose..
And I guess I'm kind of confused why I need to use an array to store the tokens- do I even need an array?.
Re: formatting text: StringTokenizer, array
int count = 0;
while (tokenizer.hasmoreelements){
count++;
System.out.println(tokenizer.nexttoken());
if(count==3){
count = 0;
System.out.println("\n");
}
something like that ?
}
Re: formatting text: StringTokenizer, array
xlomo, that basically works: prints
hihellowhow
areyoutoday
But Im still curious to figure it out the for-loop way w/ the array...
Re: formatting text: StringTokenizer, array
I like xlomo's idea better actually.
Re: formatting text: StringTokenizer, array
ok, I merged the two together...
Code:
import java.util.StringTokenizer;
public class six{
public static void main(String args[]){
String s="hi hello how are you today";
int numlines=3;
int count=0;
String [] format=new String[300];
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
for(int i=0;i<=s.length();i++)
if(count==3){
count=0;
System.out.println(" ");
}else{
count++;
System.out.print(format[i]=st.nextToken());}
Thanks to both of you for getting me there! Now I have to figure out how why I even need an array- my teacher said we had to have one
Re: formatting text: StringTokenizer, array
Your idea is dangerous.
Consider if there are only 5 tokens then you will run into an error because your loop will loop 3 times even if the StringTokenizer has run out of tokens. Better to stick with Xlomo's idea. Also please enclose all blocks in curly braces including for loops or if blocks even if the block contains only one statement.
Re: formatting text: StringTokenizer, array
I tried it with "hi hello how are you". I think thats 5 tokens and it seemed to work...unless I misunderstood
And I completely agree Xlomo's way is much better- but for the next assignment,which is sort of based off this one, the user enters a length x for the string and my teacher says we have to use an array to save the input string. I don't really get why, I'll have to ask.
Re: formatting text: StringTokenizer, array
Code:
import java.util.Scanner;
public class Exercices
{
public static void main(String[] args)
{
String st = "I usually don't post codes without explanation but in this case I think enough is said, as you can see this solution is without an array.";
int counter=0;
int length = st.length();
for(int k=0;k<st.length();k++)
{
if(counter==3)
{
System.out.println(st.substring(0, k));
st = st.substring(k,st.length());
counter = 0;
k=0;
}
if(st.charAt(k) == ' ')
counter++;
}
if(counter<3)
System.out.println(st);
}
}