Use StringTokenizer to count number of words and characters in words

12,690

Solution 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

Solution 2

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);
}
Share:
12,690
Smeaux
Author by

Smeaux

Updated on June 04, 2022

Comments

  • Smeaux
    Smeaux almost 2 years

    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!

  • Smit
    Smit almost 11 years
    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.
  • cmbaxter
    cmbaxter almost 11 years
    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.
  • Smeaux
    Smeaux almost 11 years
    Thanks! I knew I had to use hasMoreTokens() but I wasn't sure how to go about it.