mercredi 30 septembre 2020

How to determine if a char is upper or lower case - Java [closed]

I'm trying to write a program that will, for each character in a user-inputted string, count if it is uppercase and display the amount of each afterwards. This code is very messy and weird because I'm still trying to figure out what to do. Here is the code right now:

public static void main(String[] args) {
       
        Scanner sc = new Scanner(System.in);
        String sentence = sc.nextLine();
        int a = sentence.length();
        int ind = 0;
        char letter = sentence.charAt(ind);
        int k, l;
        k = 0;
        l = 0;
        
        boolean up_or_low = Character.isUpperCase(letter);
        
        while (ind < a) {
            if (up_or_low == false) {
                k += 1;
                ind += 1;
                
            } else if (up_or_low == true) {
                l += 1;
                ind += 1;
            }
        }
        
        System.out.println(k + ", " + l);
    }
}

Also, if the code looks weird it might be because it is part of a larger program but I'm writing and testing each section independently. When I run this with a scanner in the if else statement. I can see that the program returns true or false correctly, but after the first character it displays the same thing every time (the same as the first one). Please help? Btw I'm new to this language.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire