Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how do I divide a paragraph to lines a certain number of words?

801912Apr 9 2009 — edited Apr 14 2009
Hello,
I have written a class that is supposed to basically, divide the number of paragraphs (in this case separated by newlines) to lines with 10 words or less, meaning each line has 10 words until the last line, which might have fewer words.
I am using jre 1.3, and in my assignment at work I don't have the choice of changing it to a higher jre. So, I have to use 1.3.

I have explained in the code what I want to do, and what I have done. Right now, my problem is in the last for loop, where I wish to take all the words, divide them to 10-word (or less for the last line) sets and add them to the String object line. Afterwards, I'd like to add these 10 words to the Vector lines. As of now, the individual words are getting added to the Vector, instead of lines.

So, basically what I need to do is, count up to 10 words, add them to String line, and when this is finished (which is not the case now), add line to the Vector lines.

Any help will be greatly appreciated. I am really confused on how to implement this part of the code.

Here's the code:
import java.util.StringTokenizer;
import java.util.Vector;

public class StringTester {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        String str = new String("WASHINGTON (CNN) -- Vice President Joe Biden brushed aside "+
            "recent criticism by predecessor Dick Cheney that moves by the Obama " +
            "administration had put the United States at risk, telling CNN on Tuesday " +
            "that the former vice president was dead wrong.\n"+
            "I don't think [Cheney] is out of line, but he is dead wrong, he told CNN's " +
            "Wolf Blitzer. This administration -- the last administration left us in a " +
            "weaker posture than we've been any time since World War II: less regarded " +
            "in the world, stretched more thinly than we ever have been in the past, " +
            "two wars under way, virtually no respect in entire parts of the world.\n"+
            "I guarantee you we are safer today, our interests are more secure today than " +
            "they were any time during the eight years of the Bush administration."+
            "In an interview with CNN's John King last month, Cheney said President Obama " +
            "had been making some choices that in my mind will raise the risk to the " +
            "American people of another attack.");
       
	   //Basically, what I want to do is divide each of these paragraphs to lines 
          //with 10 or less words.  That is, each line has 10 words until the last line
          //which might have fewer words.      
	   StringTokenizer st = new StringTokenizer(str, "\n");
	   //1. Take each token (which is a paragraph)
	   //2. count the number of words it has
	   //3. count up to 10 words, until the word count has reached the 
	   //total number of words on each paragraph, and each of the ten words to a line.
	   Vector paragraphs = new Vector();
	   while (st.hasMoreTokens()) {
           paragraphs.addElement(st.nextToken());
	   }
	   Vector lines = new Vector();
	   int wordCount = 0;
	   Vector words = new Vector();
	   for(int i=0;i<paragraphs.size();i++) {
           StringTokenizer st2 = new StringTokenizer((String)paragraphs.elementAt(i), " ");
           //the number of tokens in st2 represents the number of words (separated by space) 
           //in each paragraph.
           while(st2.hasMoreTokens()) {
               //then add each word to an arrayList
        	   words.addElement(st2.nextToken());
           }
	   }
	   
	   for(int i=0;i<words.size();i++) {
		   String line = "";
	       while(wordCount < 10 * i) {
	    	   line = line.concat((String)words.elementAt(i));

	    	   wordCount+=10;
	       }
	       System.err.println("adding line: "+line);
    	   lines.addElement(line);
	   }
	}

} 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 12 2009
Added on Apr 9 2009
20 comments
518 views