Answer the question
In order to leave comments, you need to log in
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);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question