Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Getting length of individual token?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default (SOLVED) Getting length of individual token?

    Hello! I'm making a program that formats inputted text based on the line width the user inputs and the type of formatting the user chooses (left-justified, right-justified, centered). I'm not allowed to use arrays for this, so I decided to tokenize the string instead. The problem is, I can't make the next word go down a line after the width of the line has been reached. Here's my code:

    import java.io.*;
    import java.lang.*;
    import java.util.*;
     
    class Format
    {
      public static void main (String[]args)
      {
        String winput, sinput, finput, output;
        int winput2 = 0;
     
        Console console = System.console();
     
        winput = console.readLine("Please enter the width of the line. This must be less than or equal to 80: ");
        winput2 = Integer.parseInt(winput);
     
        while (winput2 > 80)
        {
        	winput = console.readLine("Please enter the width of the line. This must be less than or equal to 80: ");
        	winput2 = Integer.parseInt(winput);	
        }	
     
        sinput = console.readLine("Please enter the text you want to be formatted: ");
        StringTokenizer sinput2 = new StringTokenizer(sinput);
        finput = console.readLine("How do you want to format your text? Choose 'L' for left-justified, 'R' for right-justified, 'C' for centered, or 'Q' to quit the program: ");
     
        if (finput.equals("L"))
        {
          Lformat(winput2, sinput);
        }
        else if (finput.equals("R"))
        { 
          Rformat(winput2, sinput);
        }
      } 
     
      public static void Lformat (int winput, String sinput)
      { 
      	int i, count = 0;
      	for (i=1; i <= winput; i++)
      	{
      		System.out.print("a");
      	}
      	System.out.println();	
      	StringTokenizer sinput2 = new StringTokenizer(sinput);
        while(sinput2.hasMoreTokens())
        {	
          System.out.print(sinput2.nextToken() + " ");
        }	
      } 
     
      public static void Rformat (int winput, String sinput)
      { 
        System.out.println(sinput);
      }  
    }

    In other words, is it possible to get the length of each individual token so I know how much to increment the variable "count"? Being able to do so would allow me to halt the next token from being printed out on the same line if it exceeds the line width. Thanks in advance!
    Last edited by Kimimaru; February 13th, 2011 at 03:37 PM.


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Getting length of individual token?

    while(sinput2.hasMoreTokens())
    {
    System.out.print(sinput2.nextToken() + " ");
    }

    in this when you are printing string tokens , calculate length of each token and add them.
    If sum of the lengths of tokens is greater than winput use println for line break,and make sum variable = size of the token you are printing in new line.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. The Following User Says Thank You to DanBrown For This Useful Post:

    Kimimaru (February 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Getting length of individual token?

    Thanks for the feedback, but my problem was that I didn't know how to get the length of each token. What function can I use to do so?

  5. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Getting length of individual token?

    Read the API for String, in particular the length() method.

  6. The Following User Says Thank You to copeg For This Useful Post:

    Kimimaru (February 8th, 2011)

  7. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Getting length of individual token?

    I tried: "count = sinput2.length();" and "count = sinput2.length;" and it says it cannot find the symbol of length. I've used length for arrays and normal strings and it worked; I'm not sure why it's erroring up for tokens, though.

  8. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Getting length of individual token?

    You have to get the token, and then check the size.

    StringTokenizer sinput2 = new StringTokenizer(sinput);
    String temp = sinput2.nextToken();
    System.out.println(temp.length()); //There is no size() method for the String type. Instead, use length()

  9. The Following User Says Thank You to Brt93yoda For This Useful Post:

    Kimimaru (February 8th, 2011)

  10. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Getting length of individual token?

    This is what I came up with so far:

    while(sinput2.hasMoreTokens())
        {	
          String temp = sinput2.nextToken();
          System.out.println(temp.length());
          count += temp.length();
          if (count > winput)
          {
          	System.out.println();
          }	
          System.out.print(sinput2.nextToken() + " ");
        }

    The problem is, if I type something like "Hello I like to eat food," it will skip every other token (i.e. it will only consider "Hello," "like," and "eat"). This makes it impossible for me to find every individual one and calculate the total length.

  11. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Getting length of individual token?

    You need to change System.out.print(sinput2.nextToken() + " "); to System.out.println(temp + " ");

  12. #9
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Getting length of individual token?

    Okay, left-justification is working fine! Now I need to include right-justification and centered text. My right-justfication code is as follows:

    public static void Rformat(int winput, String sinput)
      {
        int i = 0, count = 0;
     
        // Print out one space to the right since we're filling the entire line
        for (i=2; i <= (winput + 1); i++)
        {
      	  System.out.print("a");
        }
     
        System.out.println();	
        StringTokenizer sinput2 = new StringTokenizer(sinput, " ");
     
        int spaces = 0; // Variable for storing how many spaces we should make
     
        String text = null, space = null;
     
        while(sinput2.hasMoreTokens())
        {    
          String temp = sinput2.nextToken();
          count += temp.length() + 1;
     
          if (count < winput)
          {
    	    text = (text + temp + " ");
          }
          else
          {
    	  // Find out how many spaces we should put on the line
            spaces = (count - winput);
     
    	  // Move all the text to the right one if the line is full
            if (spaces == 0)
            {
    	      text = (" " + text);
    	    }
    	    // Put the amount of spaces we're missing for the next word on this line
    	    else
    	    {
    	      // Add the spaces to the text
    	      for (i = 0; i <= spaces; i++)
    	      {
    	        space = (space + " ");
    	      }
     
    	       // Add the spaces to the actual text
    	       text = (space + text);
    	    }
     
    	    // Print out the text
    	    System.out.println(text);
     
    	    // Set the text to the new word
    	    text = temp;
     
    	    // Set the count to the length of the new word
    	    count = temp.length();
            }
        }
      }

    Now, the code seems fine to me, yet it doesn't work with only one word and it displays "null" 4 times along with the rest of the text when you put in more than one word. Can anyone pinpoint my mistake, please? Thanks in advance!

  13. #10
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Getting length of individual token?

    I think my dear , instead of asking others for pointing out the mistake.You should try it more than 10 times.Because this is the only way to improve your skills.
    I am feeling you are not taking enough efforts to solve your problem.
    you should not be more dependent on others.

    Sorry if i said any thing wrong.!!

    and this thing i would like to tell to every member of this forum,ask questions ,discuss your problems,but after taking enough effort to solve that problem.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  14. #11
    Junior Member
    Join Date
    Jun 2010
    Posts
    28
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Getting length of individual token?

    Thanks for the advice DanBrown.

    Unfortunately, I had to study all of last week for midterms, so I didn't have the full 2 weeks I needed to spend on this program. On top of this, I have a lot of other work that needs to be done by the end of the week.

    I'm sorry for being so dependent at the moment, but I did put forth effort into the right-justification method. I'm just running low on time and I need to finish this program by then. However, I do analyze the code that is given to me until I understand it, so it's not exactly a copy-paste thing where I have no idea what I just put into my program.

Similar Threads

  1. token-based processing
    By amandat in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2010, 12:05 AM
  2. Controlling individual threads
    By youngstorm in forum Threads
    Replies: 4
    Last Post: October 19th, 2010, 03:28 PM
  3. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM
  4. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 06:24 AM
  5. Replies: 2
    Last Post: February 4th, 2009, 11:24 AM