How do you count letters in a word?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 7084 Views - Last Post: 19 August 2010 - 02:49 AM Rate Topic: -----

#1 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

How do you count letters in a word?

Posted 18 August 2010 - 11:47 PM

I am having so much trouble trying to count the letters in a word thats in a sentence.
I was given a phrase and I am suppose to count all the letters total.
I'm thinking of making the phrase an array but then I forgot how to make it read each index/word and count the letters in it.
Could I use .length for the array indexes or tokens with .next?
Please help. I just need an example and I'll take it from there.


This was the question btw:
Write a method named wordStats that accepts as its parameter a Scanner holding a sequence of words and that reports the total number of words and the average word length. You may assume that the Scanner isn't empty. For example, suppose the Scanner is scanning an input source that contains the following words:

To be or not to be, that is the question.
For the purposes of this problem, we will use whitespace to separate words. That means that some words include punctuation, as in "be,". (This is the same definition that the Scanner uses for tokens.) For the input above, your method should produce exactly the following output:

Total words = 10
Average length = 3.2

This post has been edited by JaJEff: 18 August 2010 - 11:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do you count letters in a word?

#2 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:02 AM

I'd use a String tokenizer here... Something like:
String sentance= "This|is|a|sentance!";
StringTokenizer tokens = new StringTokenizer(sentance, "|");
int totalCharacters= 0;
int totalWords= 0;
while(tokens.hasMoreTokens()){
String token= tokens.nextToken();
totalCharacters+= token.length();
totalWords++;
}

At the end you have number of words and total number of characters stored in two variables. Other thing that comes to my mind is splitting a String:
String array[]= sentance.split("\\|");

Was This Post Helpful? 0
  • +
  • -

#3 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:05 AM

Could you give me another way? Cause I never learned tokenizer :X
I'm only in CSE 142. Btw this is just a practice problem. Studying for test tomorrow and rather not learn new material xD
Btw thanks and the question doesn't give the phrase so I cant add the | to them.
Would this work?



String sentance = console.nextLine;
StringTokenizer tokens = new StringTokenizer(sentance, " ");
int totalCharacters = 0;
int totalWords = 0;
while(tokens.hasMoreTokens()){
String token = tokens.nextToken();
totalCharacters += token.length();
totalWords++;
}

This post has been edited by JaJEff: 19 August 2010 - 12:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:11 AM

Just got an error how do I fix this?

You are referring to an identifer (a name of a variable, class, method, etc.) that is not recognized. Perhaps you misspelled it, mis-capitalized it, or forgot to declare it?
cannot find symbol
symbol : variable nextLine
location: class Scanner
String sentance = console.nextLine;
^
1 error

Btw this is the problem which I encountered like 5 times. I can't figure out how to change or fix this.

This post has been edited by JaJEff: 19 August 2010 - 12:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:12 AM

Hey whatsup,

You could also do a little something like this:

int counter = 0;
String str = "whatever"  
      
for (int i = 0; i < str.length(); i++) {

     if (Character.isLetter(str.charAt(i)))
     counter++;
}




It would then read the String/word "whatever" as having 8 characters..
Hope this kinda helps..
Was This Post Helpful? 0
  • +
  • -

#6 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:15 AM

My main problem right now is just trying to store the phrase into a string or a array so I can read each token.

This post has been edited by JaJEff: 19 August 2010 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:20 AM

View PostJaJEff, on 18 August 2010 - 11:11 PM, said:

Just got an error how do I fix this?

You are referring to an identifer (a name of a variable, class, method, etc.) that is not recognized. Perhaps you misspelled it, mis-capitalized it, or forgot to declare it?
cannot find symbol
symbol : variable nextLine
location: class Scanner
String sentance = console.nextLine;
^
1 error

Btw this is the problem which I encountered like 5 times. I can't figure out how to change or fix this.


Please read the documentation: public String nextLine()
Was This Post Helpful? 0
  • +
  • -

#8 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:25 AM

Geez what IDE you using? That's some snazzy looking Error Message.. :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#9 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:27 AM

Some random one on a practice website lol. Anyways I read the document and still at lost.
Was This Post Helpful? 0
  • +
  • -

#10 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:31 AM

Make a scanner and read a line:
Scanner sc= new Scanner(System.in);
String phrase= sc.nextLine();

Was This Post Helpful? 0
  • +
  • -

#11 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 12:44 AM

View PostMercurial, on 18 August 2010 - 11:31 PM, said:

Make a scanner and read a line:
Scanner sc= new Scanner(System.in);
String phrase= sc.nextLine();


But the question says that the method takes the scanner as a parameter.
Was This Post Helpful? 0
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How do you count letters in a word?

Posted 19 August 2010 - 01:00 AM

Given a 10 word input why are you using newline()?
Was This Post Helpful? 0
  • +
  • -

#13 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 01:02 AM

NVM FIXED IT forgot to put ()

This post has been edited by JaJEff: 19 August 2010 - 01:05 AM

Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How do you count letters in a word?

Posted 19 August 2010 - 01:05 AM

Post your code.
Was This Post Helpful? 0
  • +
  • -

#15 JaJEff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-August 10

Re: How do you count letters in a word?

Posted 19 August 2010 - 01:27 AM

This is what I got so far:

public static void wordStats (Scanner console) {
   int[] a = console.next();
   int tWords = 0;
   int tLetter = 0;
   for (int i = 0; i < a.length; i++) {
      tWords++;
      tLetter += ; // Trying to get the number of letters in the index a[i]
   }
   
   int aWords = (tWords / tLetter);
   
   System.out.println("Total words    = " + tWords);
   System.out.println("Average length = " + aWords);

}


I tweaked the above code and got this:

public static void wordStats (Scanner console) {
   String[] a = console.next(); // I think this is the problem of it not compiling
   int tWords = 0;
   int tLetter = 0;
   for (int i = 0; i < a.length; i++) {
      tWords++;
      String s = a[i];
      tLetter += s.length;
   }
   
   int aWords = (tWords / tLetter);
   
   System.out.println("Total words    = " + tWords);
   System.out.println("Average length = " + aWords);

}

This post has been edited by JaJEff: 19 August 2010 - 01:39 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2