yo, so im trying to make a program that can take string input from the user for instance: "ONCE UPON a time" and then report back how many upper and lowercase letters the string contains:
output example: the string has 8 uppercase letters the string has 5 lowercase letters, and im supposed to use string class not arrays, any tips on how to get started on this one? thanks in advance, here is what I have done so far :D!
import java.util.Scanner;
public class q36{
    public static void main(String args[]){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Give a string ");
        String input=keyboard.nextLine();
        int lengde = input.length();
        System.out.println("String: " + input + "\t " + "lengde:"+ lengde);
        for(int i=0; i<lengde;i++) {
            if(Character.isUpperCase(CharAt(i))){
            }
        }
    }
}
                Simply create counters that increment when a lowercase or uppercase letter is found, like so:
for (int k = 0; k < input.length(); k++) {
    /**
     * The methods isUpperCase(char ch) and isLowerCase(char ch) of the Character
     * class are static so we use the Class.method() format; the charAt(int index)
     * method of the String class is an instance method, so the instance, which,
     * in this case, is the variable `input`, needs to be used to call the method.
     **/
    // Check for uppercase letters.
    if (Character.isUpperCase(input.charAt(k))) upperCase++;
    // Check for lowercase letters.
    if (Character.isLowerCase(input.charAt(k))) lowerCase++;
}
System.out.printf("There are %d uppercase letters and %d lowercase letters.",upperCase,lowerCase);
                        java 8
private static long countUpperCase(String inputString) {
        return inputString.chars().filter((s)->Character.isUpperCase(s)).count();
    }
    private static long countLowerCase(String inputString) {
        return inputString.chars().filter((s)->Character.isLowerCase(s)).count();
    }
                        The solution in Java8:
private static long countUpperCase(String s) {
    return s.codePoints().filter(c-> c>='A' && c<='Z').count();
}
private static long countLowerCase(String s) {
    return s.codePoints().filter(c-> c>='a' && c<='z').count();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With