Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: confused

  1. #1
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default confused

    Hi,
    I'm new to this forum and this is my first post so I hope I'm posting in the correct spot and that I am doing it correctly. I'm really new to programming but very keen to learn. Doing an IT degree. If anyone can tell me what I have done wrong here that would be great.
    The program should count the amount of vowels that are in a string entered by the user.

    import java.util.Scanner;
    public class VowelCounter {
     
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
     
            char vowelCounter[] = new char[5];
            vowelCounter[0] = 'a';
            vowelCounter[1] = 'e';
            vowelCounter[2] = 'i';
            vowelCounter[3] = 'o';
            vowelCounter[4] = 'u';
     
            //Prompt user to input a string
            int counter = 0;
            String aString;
            System.out.println("Enter a string of text: ");
            aString = input.nextLine();
            if (aString == "")
            {
                aString = input.nextLine();
            }
     
            for (int i = 0; i < aString.length(); i++)
            {
                char charInPosition = aString.charAt(i);
     
                for (int j = i+1; j < vowelCounter.length; j++ )
                {
                    if (charInPosition == vowelCounter[j])
                    {
                        counter++;
                    }
     
                }
            }
            System.out.println(counter);
     
     
     
        }
     
    }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: confused

    Compare the contents of objects (e.g. String) with the equals() method, not '=='. In this case you can use aString.isEmpty().

    Your inner loop counter that is checking the current input string character against the vowels should start at 0. It has nothing to do with the outer loop counter 'i'.

  3. The Following User Says Thank You to dlorde For This Useful Post:

    joon (June 11th, 2011)

  4. #3
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: confused

    Thanks very much dlorde. That was an easy fix but It was beyond me.

  5. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: confused

    If you don't have a debugger or don't know how to use one, the easiest way to spot what's wrong in code like that is to step through it by hand, with pencil and paper and track the values of variables as you go. With experience, you can spot these sorts of errors just by reading the code, but knowing how to use a debugger is worth its weight in notepads and pencils...

  6. The Following User Says Thank You to dlorde For This Useful Post:

    joon (June 12th, 2011)

  7. #5
    Junior Member joon's Avatar
    Join Date
    Jun 2011
    Location
    I come from Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: confused

    Yeh I'm not too sure how to use the debugger but I will have a look online and see if I can find some tutorials. I guess I need a bit more time as well. I've only been learning for three months so I'm not really sure about everything. I have so much work to learn with uni that I don't have time to concentrate on just programming.
    Thanks for the tips and the help though.
    Last edited by joon; June 12th, 2011 at 11:42 AM.

Similar Threads

  1. I'm confused...
    By acolar in forum Object Oriented Programming
    Replies: 1
    Last Post: April 14th, 2011, 12:14 PM
  2. confused on loop
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2010, 03:26 PM
  3. Confused with Arrays
    By Solidius in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 09:54 AM
  4. Confused about setting up JSP
    By mjpam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: September 17th, 2010, 05:14 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM