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 7 of 7

Thread: Are you a JAVA ninja??? Then help me PLEASE!!!!

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Are you a JAVA ninja??? Then help me PLEASE!!!!

    Hi there,

    I've been trying to find the errors of the following code and I'm totally stuck . The program it's the usual Vowel Counter.

    If no sentence is been introduced then the program outputs the following sentence:

    Usage: java VowelCounter [SENTENCE]
    Try `java VowelCounter --help' for more information.

    If only a word is introduced then the program outputs the following sentence:

    Usage: java VowelCounter [SENTENCE]
    Count the number of vowels in SENTENCE.
    Example: java VowelCounter The quick brown fox jumps over the lazy dog

    When we introduce more than one word then program counts the number or vowels.

    I get the correct sentences when no introducing a word or only one word. When I introduce more than one word then the program does not count the vowels and I get the exception message.

    PLEASE!!!! HEEELP!!! If someone could just guide me thru the error, so I can try to solve it...

    ORIGINAL CODE

    /**
     * Class that counts the number of vowels in a sentence.
     */
    public class VowelCounter {
     
    /** Flag to show the help of this program. */
    private static final String HELP_FLAG  = "--help";
    /** How to use this program. */
    private static final String USAGE_LINE = "Usage: java VowelCounter [SENTENCE]";
    /** The sentence which vowels are being counted. */
    private String              sentence;
     
    /**
     * Constructor.
     * @param words the words of the sentence which vowels will be counted
     */
    public VowelCounter(final String[] words) {
        super();
        String space;
        String phrase= "";
        char x= 0;
        while (x < words.length) {
            if (x == 0) {
                space= "";
            } else {
                space= " ";
            }
            phrase= phrase + space + words[x];
            x= x + 1;
        }
        this.sentence= phrase;
    }
     
    /**
     * Getter.
     * @return the sentence which vowels are being counted
     */
    public String getSentence() {
        return this.sentence;
    }
     
    /**
     * It counts the number of characters in the sentence.
     * @param c the character to count in the sentence
     * @return the occurrences of character 'c' in the sentence (case-insensitive)
     */
    public int countNumberOf(char c) {
        int occurrences= 0;
        int x= 0;
        while (x <= this.getSentence().length()) {
            if (Character.toLowerCase(x) == Character.toLowerCase(this.getSentence().charAt(x))) {
                occurrences= occurrences + 1;
            }
            x= x + 1;
        }
        return occurrences;
    }
     
    /**
     * Main method.
     * @param args arguments
     */
    public static void main(String[] args) {
        if (args.length() < 1) {
            System.out.println(USAGE_LINE);
            System.out.println("Try `java VowelCounter --help' for more information.");
        } else if (args.length == 1 && !HELP_FLAG.equals(args[0])) {
            System.out.println(USAGE_LINE);
            System.out.println("Count the number of vowels in SENTENCE.");
            System.out.println("Example: java VowelCounter The quick brown fox jumps over the lazy dog");
        } else {
            try {
                VowelCounter vc= new VowelCounter(args[0]);
                System.out.println("The sentence '" + vc.getSentence() + "' has:");
                System.out.println(vc.countNumberOf("a") + " 'A' vowels.");
                System.out.println(vc.countNumberOf('e') + " 'E' vowels.");
                System.out.println(vc.countNumberOf('i') + " 'I' vowels.");
                System.out.println(vc.countNumberOf('o') + " 'O' vowels.");
                System.out.println(vc.countNumberOf('u') + " 'U' vowels.");
            } catch (Exception ex) {
                System.out.println("Got error '" + ex.getMessage() + "'. Check you input and try again");
            }
        }
    }
     
    }


    MY CODE

    /**
     * Class that counts the number of vowels in a sentence.
     */
    public class VowelCounter {
     
    /** Flag to show the help of this program. */
    private static final String HELP_FLAG  = "--help";
    /** How to use this program. */
    private static final String USAGE_LINE = "Usage: java VowelCounter [SENTENCE]";
    /** The sentence which vowels are being counted. */
    private String              sentence;
     
    /**
     * Constructor.
     * @param words the words of the sentence which vowels will be counted
     */
    public VowelCounter(final String[] words) {
        super();
        String space;
        String phrase= "";
        int x= 0;//error
        while (x < words.length) {
            if (x == 0) {
                space= "";
            } else {
                space= " ";
            }
            phrase= phrase + space + words[x];
            x= x + 1;
     
        }
     
        this.sentence= phrase;
    }
     
    /**
     * Getter.
     * @return the sentence which vowels are being counted
     */
    public String getSentence() {
        return this.sentence;
    }
     
    /**
     * It counts the number of characters in the sentence.
     * @param c the character to count in the sentence
     * @return the occurrences of character 'c' in the sentence (case-insensitive)
     */
    public int countNumberOf(char c) {
        int occurrences= 0;
        int x= 0;
        while (x <= this.getSentence().length()) {
            if (Character.toLowerCase(x) == Character.toLowerCase(this.getSentence().charAt(x))) {
                occurrences= occurrences + 1;
            }
            x= x + 1;
        }
        return occurrences;
    }
     
    /**
     * Main method.
     * @param args arguments
     */
    public static void main(String[] args) {
     
     
        if (args.length < 1) {//Error
     
     
            System.out.println(USAGE_LINE);
            System.out.println("Try `java VowelCounter --help' for more information.");
     
     
        } else if ((args.length == 1) && !(HELP_FLAG.equals(args[0]))) {//Error
     
     
            System.out.println(USAGE_LINE);
            System.out.println("Count the number of vowels in SENTENCE.");
            System.out.println("Example: java VowelCounter The quick brown fox jumps over the lazy dog");
     
        } else {
            try {
     
     
                VowelCounter vc= new VowelCounter(args);//Error
     
     
                System.out.println("The sentence '" + vc.getSentence() + "' has:");
     
     
     
     
                System.out.println(vc.countNumberOf('a') + " 'A' vowels.");//Error            
                System.out.println(vc.countNumberOf('e') + " 'E' vowels.");
                System.out.println(vc.countNumberOf('i') + " 'I' vowels.");
                System.out.println(vc.countNumberOf('o') + " 'O' vowels.");
                System.out.println(vc.countNumberOf('u') + " 'U' vowels.");    
     
            } catch (Exception ex) {
                System.out.println("Got error '" + ex.getMessage() + "'. Check you input and try again");
            }
        }
    }
     
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Are you a JAVA ninja??? Then help me PLEASE!!!!

    while (x <= this.getSentence().length()) << Problem.

    As indices start at 0 in Java, <= is going to go over the range of the String when you call .charAt(x).
    Test for LESS THAN only.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Are you a JAVA ninja??? Then help me PLEASE!!!!

    I get the exception message
    guide me thru the error
    Please copy and paste here the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Are you a JAVA ninja??? Then help me PLEASE!!!!

    Quote Originally Posted by newbie View Post
    while (x <= this.getSentence().length()) << Problem.

    As indices start at 0 in Java, <= is going to go over the range of the String when you call .charAt(x).
    Test for LESS THAN only.

    BINGO!!!

    Thank you sooooo much
    . That was the error.

    I don't understant the reason why using "<=" will make the 'WHILE' loop go over the range of the String, when 'x' reaches the same amount as characters have the sentence, it should jump to the "RETURN OCCURRENCES, rigth?.
    I do understant that using only the "LESS THAN" will do the job, since when it passes over the character number of the sentence will jump to "RETURN OCURRENCES".

    Here is my code with all errors fixed.

    /**
     * Class that counts the number of vowels in a sentence.
     * @author OOPD teaching staff
     */
     
    public class VowelCounter {//*******************************ERROR - fixed - ****************
     
    /** Flag to show the help of this program. */
    private static final String HELP_FLAG  = "--help";
     
    /** How to use this program. */
    private static final String USAGE_LINE = "Usage: java VowelCounter [SENTENCE]";
     
    /** The sentence which vowels are being counted. */
    private String              sentence;
     
    /**
     * Constructor.
     * @param words the words of the sentence which vowels will be counted
     */
    public VowelCounter(final String[] words) {
     
    	super();
        String space;
        String phrase= "";
     
        int x;//***************************************ERROR - fixed - ***************************
        	x=0;
        while (x < words.length) {
            if (x == 0) {
                space= "";
            } else {
                space= " ";
            }
            phrase= phrase + space + words[x];
            x= x + 1;
        }
        this.sentence= phrase;
    }
     
    /**
     * Getter.
     * @return the sentence which vowels are being counted
     */
    public String getSentence() {
        return this.sentence;
    }
     
    /**
     * It counts the number of characters in the sentence.
     * @param c the character to count in the sentence
     * @return the occurrences of character 'c' in the sentence (case-insensitive)
     */
    public int countNumberOf(char c) {
     
    	int occurrences= 0;
        int x= 0;
     
        while (x < this.getSentence().length()) {//**************ERROR - fixed ***************************
     
        	if (Character.toLowerCase(c) == Character.toLowerCase(this.getSentence().charAt(x))) {//******ERROR - fixed***********
                occurrences= occurrences + 1;
            }
            x= x + 1;
     
        }
        return occurrences;
    }
     
    /**
     * Main method.
     * @param args arguments
     */
    public static void main ( String[] args ) {
     
    	if (args.length < 1) {//****************ERROR - fixed -*************************
     
    		System.out.println(USAGE_LINE);
            System.out.println("Try `java VowelCounter --help' for more information.");
     
    	} else if ((args.length == 1) && !(HELP_FLAG.equals(args[0])))
    		{
     
            System.out.println(USAGE_LINE);
            System.out.println("Count the number of vowels in SENTENCE.");
            System.out.println("Example: java VowelCounter The quick brown fox jumps over the lazy dog");
     
    	} else {
            try {
     
            	VowelCounter vc= new VowelCounter(args);//*****************ERROR - fixed ******************
     
            	System.out.println("The sentence '" + vc.getSentence() + "' has:");
     
     
     
                System.out.println(vc.countNumberOf('a') + " 'A' vowels.");//****************ERROR - fixed ***************
     
                System.out.println(vc.countNumberOf('e') + " 'E' vowels.");
                System.out.println(vc.countNumberOf('i') + " 'I' vowels.");
                System.out.println(vc.countNumberOf('o') + " 'O' vowels.");
                System.out.println(vc.countNumberOf('u') + " 'U' vowels.");
     
            } catch (Exception ex) {
     
            	System.out.println("Got error '" + ex.getMessage() + "'. Check you input and try again");
            }
        }
    }
     
    }

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Are you a JAVA ninja??? Then help me PLEASE!!!!

    Hi Norm,

    I was getting the following error:

    The sentence 'In English, nouns must be preceded by an article.' has:
    Got error 'String index out of range: 49'. Check you input and try again


    As newie suggest it, the issue was the "<=" from the "while" loop.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Are you a JAVA ninja??? Then help me PLEASE!!!!

    The String "hello" has a length of 5.
    As indices in Java start at 0, all the valid ranges are: 0 (h), 1 (e) , 2 (l) , 3 (l) , 4 (o).

    If you let X reach a value equal to the length of the String, i.e. 5, then it is out of the range of the String.
    With that in mind, if you did let it go to 5, it would be the 6th index: 0,1,2,3,4,5.

    Hello is only 5 characters long, so what would the 6th reference be except for "Out of range".
    Hope that clears it up for you.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Are you a JAVA ninja??? Then help me PLEASE!!!!

    Thank you so much for the explanation.

Tags for this Thread