finding consonants and vowels?!

for class i have to make this program that when a user enters a sentence(using the keyboard class: Keyboard.readString();) the program lists how many vowels, consonants and spaces were in that sentence....i have no idea how to get started on this one..any ideas?
[269 byte] By [AlexGM16a] at [2007-10-3 3:48:28]
# 1
For a start, a Java program has to have a static method with a certain name and certain parameters. Do you know what that method is? Start there.
DrClapa at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 2
nope i have no idea what method i need to use for this...wait is it StringTokenizer class maybe?Message was edited by: AlexGM16
AlexGM16a at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 3
Sounds like you need to begin at the beginning. http://java.sun.com/docs/books/tutorial/
dcmintera at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 4
argh ok im on a deadline thought i got to finish this by tomorrow. I tried the tokenizer class but it just distinguishes between words and characters.
AlexGM16a at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 5
What is this "Keyboard" class?
billyChilla at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 6

Alex,

If you are truly this lost already, then you must have missed class, lectures, or have not read the required material. dcminter gave you a very good link to get you started, but if you are not willing to help yourself understand, then neither are we. Deadline or not, you still have to learn the very basics before it will make any sense and that is something we cannot do for you.

Les

morgalra at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 7

What is this "Keyboard" class?

It's some non-standard IO stuff. I'm not sure where it originates, but it seems to be used in lots of "introduction to Java" type university courses. Many of the homework questions mention it.

It may be [url http://duke.csc.villanova.edu/jss/keyboard.html]this one[/url].

dcmintera at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 8

public class LetterCounter extends Thread

{

public void run()

{

String sentence = (Keyboard.readString()).toUpperCase();

int spaceCnt = 0, vowelCnt = 0, conCnt = 0;

for(int i=0;i< sentence.length();i++)

{

char c = sentence.charAt(i);

int n = Character.getNumericValue(c);

if (Character.isLetter(c))

{

if (n == 10 || n == 14 || n == 18 || n == 24 || n == 30)

vowelCnt++;

else

conCnt++;

}else if(Character.isWhitespace(c))

spaceCnt++;

}

System.out.println("Vowels: " + vowelCnt + "\nConsonants: " + conCnt + "\nSpaces: " + spaceCnt);

}

public static void main(String [] args)//starting point for Java progras

{

Thread t = new LetterCounter();

t.start();

}

}

Let me know how it works out.

billyChilla at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 9
Billy, why are you providing him with complete code? I didn't check if it works, but you shouldn't give him a complete program like that.Besides, you shouldn't extend Thread, and a separate Thread object isn't even needed here (so changing to implement Runnable is pointless,
doremifasollatidoa at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 10
I learned through example when I was starting out. I figure AlexGM16 can look through the code and take what he needs. I think we have all used other code to help ourselves.
billyChilla at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 11

Don't be so critical. People learn from examples and that's what Billy is trying to provide here. I mean sure, he could just copy that code and turn it in without looking at it, and sure, the professor wouldn't suspect a thing since it is *obviously * using only basic Java features. ..

I think that AlexGM16 would do well to take this code and turn it in as is. He's obviously frustrated by his Java experience. We wouldn't want him to get discouraged!

quaidbrowna at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 12

thanks billy...and no i didnt copy the code because i also have to look for commas, but i just wanted to learn how to approach it. I learned a lot more by looking at this example, asking myself what this line of code meant, looked it up, and continued. I think learning by example is a great way to approach programming. Plus my programming teacher has to teach a business systems and tech class, programming 1 and 2 at the same time while she is teaching me (programming ap)

thanks a lot billy

o yeah and keyboard class is sort of a classroom package (cs1 package i believe) that hides a lot of the I/O method stuff to make it easy for you to get data from a client.

Message was edited by:

AlexGM16

AlexGM16a at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 13
Alex, I would take out the Thread stuff from that code. I dare say you have not covered Threads yet. As you appear to be struggling with some basics, Threads are too advanced. Therefore if you hand it in, the teacher will no doubt know it is not your code.
floundera at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 14

aha i get it! Using the for loop it gets the values for every character entered in the string. Then it does an if statement to check it its a vowel, otherwise its a consonant. Then the isWhitespace counts for spaces.

One question where do i go to find the list of values for each string entered by a keyboard?

AlexGM16a at 2007-7-14 21:45:30 > top of Java-index,Java Essentials,New To Java...
# 15
eg, http://www.geocities.com/rmlchan/vowelsrc.html
mchan0a at 2007-7-21 10:18:07 > top of Java-index,Java Essentials,New To Java...
# 16

> One question where do i go to find the list of values for each string entered by a keyboard?

If you're using the Keyboard class (download zip file from http://duke.csc.villanova.edu/jss/keyboard.html),

you can use its readWord() method.

If you have Java 5.0, the Scanner class has useDelimeter() and next() methods.

ValentineSmitha at 2007-7-21 10:18:07 > top of Java-index,Java Essentials,New To Java...
# 17

> One question where do i go to find the list of values

> for each string entered by a keyboard?

Not sure I understand you correctly but your Keyboard class has a method to read and return a String that was type in. You then pass that String to your method that loops over the String and counts the consonants, vowels, spaces etc.

floundera at 2007-7-21 10:18:07 > top of Java-index,Java Essentials,New To Java...
# 18
By the way, rather than using "getNumericValue", a direct comparison of the char to one of the five vowels seems more straightforward:if (c == 'A' || c == 'E' || ...)
doremifasollatidoa at 2007-7-21 10:18:07 > top of Java-index,Java Essentials,New To Java...
# 19
Why is the code in reply 8 in a Thread at all? As pointed out you shoud be implementing Runnable and not extending thread but what is the point of starting a new thread in that code anyway? None.
cotton.ma at 2007-7-21 10:18:07 > top of Java-index,Java Essentials,New To Java...