Results 1 to 3 of 3
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Question Just a little help with some code?

    I have an assignment in my class where I need to make a program that prompts you for input, then finds the consonants and vowels and displays the number of each. the best I could come up with is to find one of either the consonants and vowels, and subtract that from the total number of letters to find the other. I've thought of using each vowel as a char. array to keep it simple, but I'm having trouble incorporating the array into the math, along with accounting for spaces and punctuation.
    here's my code so far:

    import java.util.*;
    public class VowCons {
    public static void main(String[] args) {
    String input = "";

    char[] vowelArray = {'a','e','i','o','u'};

    Scanner words = new Scanner(System.in);
    System.out.println("Enter a string:");
    input+=words.nextLine();

    int characters = input.length();
    int vowels=0;
    int consonants=characters - vowels;

    System.out.println("There are " +vowels+ " vowels");
    System.out.println("There are " +consonants+ " consonants");

    words.close();
    }
    }

    The main thing I'm having trouble with is the value of the vowels variable (mouthful, right?) to have something to properly subtract from. Thanks in advance.
    Edit- I forgot to put it in, but I've been using if statements, but to no avail.
    Last edited by herpeslurpy; 12-08-2012 at 09:10 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Just a little help with some code?

    You need to loop through the char's of the String with a for loop, checking each char to see if it's a vowel or not. Please give that a go and let us know what you come up with.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Just a little help with some code?

    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •