Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By xlomo

Thread: formatting text: StringTokenizer, array

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: formatting text: StringTokenizer, array

    Quote Originally Posted by katiebear128 View Post
    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.

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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?.

  4. #4
    xlomo is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default 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 ?
    }
    Fubarable likes this.

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: formatting text: StringTokenizer, array

    I like xlomo's idea better actually.

  7. #7
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: formatting text: StringTokenizer, array

    ok, I merged the two together...
    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;
        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
    Last edited by katiebear128; 11-05-2011 at 09:56 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  9. #9
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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.

  10. #10
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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 02:39 AM.

Similar Threads

  1. Text file to 2d array
    By Mrussell9 in forum New To Java
    Replies: 1
    Last Post: 05-07-2011, 08:05 PM
  2. RTF (rich text format) Formatting
    By runedog48 in forum Advanced Java
    Replies: 6
    Last Post: 04-20-2011, 12:43 AM
  3. Text formatting and alignment according to the printer
    By java_fledgeling in forum Advanced Java
    Replies: 2
    Last Post: 06-08-2010, 11:34 AM
  4. Replies: 1
    Last Post: 03-16-2009, 07:05 PM
  5. Testing StringTokenizer Tokens against a text pattern
    By everlast88az in forum New To Java
    Replies: 5
    Last Post: 11-05-2008, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •