Click here to Skip to main content
15,883,955 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im suppose to make a code out of this but my ideas and programming skill arent fit for this, i would hoping to lend aid if thats ok


Create a Java application that will readily count (listed below) based on the supplied main() String array argument.

total characters (visible and invisible)
number of vowels
number of consonants
number of Uppercase letters
number of lowercase letters
total number of embedded word search
number of invisible characters

What I have tried:

package charactercountandembeddedwordsearch;
 
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("argument input : ");
 
        // 1
        String str = scanner.nextLine();
        scanner.close();
 
        // 2
        str = str.toLowerCase();
 
        int vCount = 0, cCount = 0;  
        //3
        for (int i = 0; i < str.length(); i++) 
        {
            //4. Checks whether a character is a vowel
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                               || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
                // Increments the vowel counter
                vCount++;
            }
            //5. Checks whether a character is a consonant
            else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') 
            {
                // Increments the consonant counter
                cCount++;
            }
        }
        //6
        System.out.println("Number of vowels: " + vCount);
        System.out.println("Number of consonants: " + cCount);
    }
}
Posted
Updated 18-Oct-21 4:10am
Comments
Richard MacCutchan 18-Oct-21 9:40am    
Your consonant check is incorrect, as it just checks for an alphabetic character. You should first check that the character is alphabetic, i.e. in the range[ a:z ]. Once you have that then test if it is a vowel, and if so add 1 to the vowel count, and if not add 1 to the consonant count. Similarly you can test for upper and lower case. I am not sure what is meant by the final two questions.

1 solution

Start by thinking about how you would do it manually: you might start by classifying characters into groups: alphabetic, numerals, punctuation, word terminators (space, newline), other.

Then process each group: Alphabetic wudl be two groups: upper case and lower case, so count those separately. Alphabetic would also be consonants and vowels, so they need counting separately. Numeric digits - count them. And so forth.

When you've worked out how you would approach it manually, start thinking about implementation: do a little bit, test it, and then move on to the next.

Buty your code doesn't do that: it immediately ignores part of the task by converting the input all to lowercase - which means you can't count t6he uppercase characters!

This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900