|
-
November 9th, 2011, 09:07 PM
#1
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);
}
}
-
November 10th, 2011, 07:29 AM
#2
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: ");
-
November 10th, 2011, 08:15 AM
#3
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.
-
November 10th, 2011, 09:57 AM
#4
Re: Vowel Count - Do While + Switch Statement
.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.
Use if(total=="a"||"e"){} etc..
That's not correct. You would need to do if ( letter == 'a' || letter == 'e' || etc etc ) {}
-
November 10th, 2011, 01:10 PM
#5
Re: Vowel Count - Do While + Switch Statement
to be sincere i havent read the whole thing-.-
-
November 10th, 2011, 01:20 PM
#6
Re: Vowel Count - Do While + Switch Statement
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.
-
November 10th, 2011, 01:37 PM
#7
Re: Vowel Count - Do While + Switch Statement
i am sorry keang 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
-
November 11th, 2011, 09:41 AM
#8
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
-
November 11th, 2011, 09:47 AM
#9
Re: Vowel Count - Do While + Switch Statement
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.
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.
-
November 11th, 2011, 07:09 PM
#10
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: ");
-
November 12th, 2011, 06:43 AM
#11
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).
-
November 13th, 2011, 06:18 PM
#12
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]
Last edited by mwardcguru; November 13th, 2011 at 06:39 PM.
-
November 14th, 2011, 09:00 AM
#13
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|