W
W
wolverine7772021-10-27 15:36:45
Java
wolverine777, 2021-10-27 15:36:45

How to distinguish letters and CAPITAL LETTERS from everything else in a list?

Hello, I don’t understand how to distinguish array elements:

public static void main(String[] args) {

        List myList = new ArrayList();
        Scanner sc = new Scanner(System.in);
        int countCapitals = 0;
        int countCharacters = 0;
        char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        char[] majus = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();


        for (int i=0; i<3; i++) {
            System.out.println("object "+(i+1)+":");
            String num = sc.next();
            myList.add(num);
        }

        System.out.println(myList.contains("t")); // to check if list contains "t"

        for (int x=0; x<myList.size(); x++) {
            System.out.println(myList.get(x));
            // если правильно понимаю тут должен быть if но как его сделать?

        }
        System.out.println("countCharacters = " + countCharacters);
        System.out.println("countCapitals = " + countCapitals);
    }


Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey_USB, 2021-10-27
@Sergey_USB

Open the table of ASCII codes. Everything else should just be.

C
Constable, 2021-10-27
@Constable

Regular expressions are here for accuracy. I suggest counting like this:

Pattern patternUppercase = Pattern.compile("[A-Z]");
Pattern patternLowercase = Pattern.compile("[a-z]");
int countCapitals = 0;
int countLowcaseCharacters = 0;
Matcher m = patternUppercase.matcher(string);
while (m.find()) {
            countCapitals++;
}
m = patternLowercase.matcher(string);
while (m.find()) {
            countLowcaseCharacters++;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question