Results 1 to 2 of 2

Math Help - More Java Help

  1. #1
    Junior Member
    Joined
    Jul 2006
    Posts
    45

    More Java Help

    This one I can't figure for the life of me--the program is supposed to prompt the user to input a sequence of characters and should output the number of vowels. For example, adsfoiu so, output is 4.

    Not sure what I'm doing wrong??

    public class VowelCount {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

    char ch;
    int counter = 0;
    String str;

    System.out.print("Enter a string of characters: ");
    System.out.flush();
    str = console.next();
    System.out.println();

    int len = str.length();
    for(int i = 0; i <= (len - 1); i++) {

    ch = console.next().charAt(0);
    if (isVowel(ch) == true)
    counter++;

    console.next().charAt(0);

    }

    System.out.println("The number of vowels in the string is: " + counter);

    }
    public static boolean isVowel(char c) {
    switch(c) {
    case 'a': case 'e': case 'i': case 'o': case 'u':
    case 'A': case 'E': case 'I': case 'O': case 'U':
    return true;
    }
    return false;


    }

    }
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Senior Member
    Joined
    Apr 2006
    Posts
    399
    Awards
    1
    Quote Originally Posted by harold View Post
    This one I can't figure for the life of me--the program is supposed to prompt the user to input a sequence of characters and should output the number of vowels. For example, adsfoiu so, output is 4.

    Not sure what I'm doing wrong??

    public class VowelCount {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

    char ch;
    int counter = 0;
    String str;

    System.out.print("Enter a string of characters: ");
    System.out.flush();
    str = console.next();
    System.out.println();

    int len = str.length();
    for(int i = 0; i <= (len - 1); i++) {

    ch = console.next().charAt(0);
    if (isVowel(ch) == true)
    counter++;

    console.next().charAt(0);

    }

    System.out.println("The number of vowels in the string is: " + counter);

    }
    public static boolean isVowel(char c) {
    switch(c) {
    case 'a': case 'e': case 'i': case 'o': case 'u':
    case 'A': case 'E': case 'I': case 'O': case 'U':
    return true;
    }
    return false;


    }

    }
    These statements

    Code:
    ch = console.next().charAt(0);
    
    console.next().charAt(0);
    are not doing what you think. Replace the first with

    Code:
    ch = str.charAt(i);
    and delete the second one.

    Now to be able to debug your own programs, you need a way to check that the program is doing what you think it is doing. There are two ways to go:

    • use a debugger to step through the program and show you the values of the variables and what functions are being executed, or
    • put in print statements. For example, if you just printed out the value of ch within the loop, you would see it does not have the values you think. And if you printed out str, you'd see it does have the values you want.

    Debuggers are good if you have one and know how to use it. Otherwise, start putting in print statements when you are puzzled by the behavior of a program.

    And by the way, the need to be skilled in debugging programs does not go away with experience. The programs just get more complicated, the mistakes more subtle, and the need to be skilled in debugging increases.
    Last edited by JakeD; October 27th 2006 at 08:04 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Java Blue J
    Posted in the Math Software Forum
    Replies: 4
    Last Post: September 5th 2010, 11:28 AM
  2. Help with Computer Science Java
    Posted in the Advanced Math Topics Forum
    Replies: 1
    Last Post: October 1st 2009, 05:24 PM
  3. Manipulate Math with Java
    Posted in the Math Forum
    Replies: 0
    Last Post: September 27th 2009, 11:24 AM
  4. Another Java Program
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: November 2nd 2006, 01:23 PM
  5. Java Program - Summing Integers
    Posted in the Advanced Math Topics Forum
    Replies: 13
    Last Post: October 28th 2006, 05:19 AM

Search Tags


/mathhelpforum @mathhelpforum