Vowel Count - Do While + Switch Statement
Error:
VowelsEnteredUntilFullStop.java:33: possible loss of precision
found : int
required: char
total = total + letter;
^
1 error
// Practical 8 - Q6
// Marcus Ward
// 07/11/2011
/* Program will allow user to repeatedly enter letters.Using a do-while loop and switch
statement, the program will count the number of vowels entered until a full stop is entered. */
import java.util.Scanner;
public class VowelsEnteredUntilFullStop
{
public static void main(String[] args)
{
Scanner keyboardIn = new Scanner(System.in);
char letter = 1, vowel, vowelCount =0, total =0; // declare variables
System.out.println("Enter a letter: "); // get user input
letter = keyboardIn.next().charAt(0);
do
{
System.out.println("Enter a letter: ");
}
while(letter != '.');
switch (total)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
total = vowelCount+ letter;
vowelCount++;
}
System.out.println("The number of vowels is" + vowelCount);
}
}
Re: Vowel Count - Do While + Switch Statement
Please use code tags when posting code.
The compile error is because when you add chars they are automatically cast to ints but the result which is an int can't be automatically cast back to a char as it could result in loss of data ie the value in the int could be to big to fit into a char.
I wouldn't worry too much about that issue at the moment because you have other more important problems to fix which will remove the above problem, for example:
What is 'total' supposed to be doing and why are you using it in a switch statement?
Why are you declaring 'vowel' and then never using it?
Why is vowelCount a char, surely it should be an int?
Why do you have an infinite loop around System.out.println("Enter a letter: ");
Re: Vowel Count - Do While + Switch Statement
hmm..im pretty new as well but i know case here is pretty inefficient. Use if(total=="a"||"e"){} etc.. it much easier like that.
Re: Vowel Count - Do While + Switch Statement
Quote:
.im pretty new as well but i know case here is pretty inefficient
You are right that using a switch is an unusual way of solving this sort of problem but I don't know if it really is a less efficient way of doing it.
Quote:
Use if(total=="a"||"e"){} etc..
That's not correct. You would need to do if ( letter == 'a' || letter == 'e' || etc etc ) {}
Re: Vowel Count - Do While + Switch Statement
to be sincere i havent read the whole thing-.-
Re: Vowel Count - Do While + Switch Statement
Quote:
to be sincere i havent read the whole thing-.-
Well don't post an answer unless you have read the whole thread and are reasonably confident you can provide an accurate answer, providing your best guess (unless you add a caveat to that effect) is not good enough and only adds to the OP's confusion.
Think about how you would feel if someone answered one of your posts with misleading information.
Re: Vowel Count - Do While + Switch Statement
i am sorry keang:D i dont come from a proffesionist community. Where i come from the users posted abstract information and the coders had to figure out what's going on. I'll be more accurate in the following times:D
Re: Vowel Count - Do While + Switch Statement
To be honest keang I'm confused about the question as I'm only a beginner. I've got rid of vowel and made vowelCount an int.
I was told in the question that I must use a Do-While Loop and a Switch Statement.
This is the error I'm now getting.
VowelsEnteredUntilFullStop.java:18: ';' expected
System.out.println("Enter a letter: "); // get user input
^
1 error
Re: Vowel Count - Do While + Switch Statement
Quote:
I was told in the question that I must use a Do-While Loop and a Switch Statement.
That's fine, it's a perfectly legitimate way of doing it.
Quote:
This is the error I'm now getting.
Without seeing the code it's hard to give any information other than the error says line 18 is missing a terminating semi-colon.
Re: Vowel Count - Do While + Switch Statement
This is what I have in line 18, buy can't see anything wrong with it :
System.out.println("Enter a letter: ");
Re: Vowel Count - Do While + Switch Statement
It's probably the line before 18 that is missing the semi-colon. Can you post all your code please (in code tags - see blue text below).
Re: Vowel Count - Do While + Switch Statement
I found the missing ';' but am now getting this error :
VowelsEnteredUntilFullStop.java:34: possible loss of precision
found : int
required: char
total = vowelCount + letter;
^
1 error
// Practical 8 - Q6
// Marcus Ward
// 07/11/2011
/* Program will allow user to repeatedly enter letters.Using a do-while loop and switch
statement, the program will count the number of vowels entered until a full stop is entered. */
[code] import java.util.Scanner;
public class VowelsEnteredUntilFullStop
{
public static void main(String[] args)
{
Scanner keyboardIn = new Scanner(System.in);
char letter = 1, total = 0; // declare variables
int vowelCount = 0;
System.out.println("Enter a letter: "); // get user input
letter = keyboardIn.next().charAt(0);
do
{
System.out.println("Enter a letter: ");
}
while(letter != '.');
switch (letter)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
total = vowelCount + letter;
vowelCount++;
}
System.out.println("The number of vowels is" + vowelCount);
}
} [code]
Re: Vowel Count - Do While + Switch Statement
Re-read my first reply in this thread.
BTW the terminating code tag needs a leading forward slash.