CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2011
    Posts
    6

    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);
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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: ");
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Posts
    189

    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.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 ) {}
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    189

    Re: Vowel Count - Do While + Switch Statement

    to be sincere i havent read the whole thing-.-

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Nov 2011
    Posts
    189

    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

  8. #8
    Join Date
    Nov 2011
    Posts
    6

    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

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Nov 2011
    Posts
    6

    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: ");

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Nov 2011
    Posts
    6

    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.

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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
  •  





Click Here to Expand Forum to Full Width

Featured