Results 1 to 10 of 10
- 11-05-2011, 07:36 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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
Java 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
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.
- 11-05-2011, 08:15 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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?.
- 11-05-2011, 08:19 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
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 ?
}
- 11-05-2011, 08:30 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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.
- 11-05-2011, 08:50 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: formatting text: StringTokenizer, array
ok, I merged the two together...
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 oneJava 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());}Last edited by katiebear128; 11-05-2011 at 08:56 PM.
-
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.
- 11-05-2011, 09:53 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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.
- 11-06-2011, 01:36 AM #10
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: formatting text: StringTokenizer, array
Java 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); } }Last edited by tnrh1; 11-06-2011 at 01:39 AM.
Similar Threads
-
Text file to 2d array
By Mrussell9 in forum New To JavaReplies: 1Last Post: 05-07-2011, 07:05 PM -
RTF (rich text format) Formatting
By runedog48 in forum Advanced JavaReplies: 6Last Post: 04-19-2011, 11:43 PM -
Text formatting and alignment according to the printer
By java_fledgeling in forum Advanced JavaReplies: 2Last Post: 06-08-2010, 10:34 AM -
[SOLVED] *Simple* Eclipse Text Formatting Question
By davomyster in forum EclipseReplies: 1Last Post: 03-16-2009, 06:05 PM -
Testing StringTokenizer Tokens against a text pattern
By everlast88az in forum New To JavaReplies: 5Last Post: 11-05-2008, 09:43 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks