2

The objective is to get a sentence input from the user, tokenize it, and then give information about the first three words only (word itself, length, and then average the first 3 word lengths). I'm not sure how to turn the tokens into strings. I just need some guidance - not sure how to proceed. I've got this so far:

public static void main(String[] args) {

    String delim = " ";

    String inSentence = JOptionPane.showInputDialog("Please enter a sentence of three or more words: ");

    StringTokenizer tk = new StringTokenizer(inSentence, delim);

    int sentenceCount = tk.countTokens();


    // Output
    String out = "";
    out = out + "Total number of words in the sentence: " +sentenceCount +"\n";

    JOptionPane.showMessageDialog(null, out);


}

I'd really appreciate any guidance!

Smeaux
  • 71
  • 1
  • 2
  • 6

3 Answers3

1

If you just wanted to get the first 3 tokens, then you could do something like this:

String first = tk.nextToken();
String second = tk.hasMoreTokens() ? tk.nextToken() : "";
String third = tk.hasMoreTokens() ? tk.nextToken() : "";

From there should be pretty easy to calculate the other requirements

cmbaxter
  • 35,283
  • 4
  • 86
  • 95
1
public static void main(String[] args) {

    String delim = " ";

    String inSentence = JOptionPane.showInputDialog("Please enter a sentence of three or more words: ");

    StringTokenizer tk = new StringTokenizer(inSentence, delim);

    int sentenceCount = tk.countTokens();

    // Output
    String out = "";
    out = out + "Total number of words in the sentence: " +sentenceCount +"\n";

    JOptionPane.showMessageDialog(null, out);

    int totalLength = 0;
    while(tk.hasMoreTokens()){
        String token = tk.nextToken();
        totalLength+= token.length();
        out = "Word: " + token + " Length:" + token.length();
        JOptionPane.showMessageDialog(null, out);
    }

    out = "Average word Length = " + (totalLength/3);
    JOptionPane.showMessageDialog(null, out);
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    Its really a good answer, but I dont think its good idea to give whole answer as OP question seems like homework. Moreover OP asking for guidance. – Smit May 24 '13 at 00:48
  • It also misses the requirements as he only wanted the average of the first three words, and the average will be wrong because he's looping over all words, but only dividing by 3 for the average. Either break the loop at 3 or divide by sentenceCount. – cmbaxter May 24 '13 at 00:58
0

The way to get the individual strings by using nextToken().

while (tk.hasMoreTokens()) {
  System.out.println(st.nextToken());
}

You're free to do anything else than printing them, of course. If you only want the three first tokens, you might not want to use a while loop but a couple of simple if statements.

Victor Sand
  • 2,270
  • 1
  • 14
  • 32