Ok i have to pass a string to the main method which is obviously stored in args[0].Then count the amount of times an uppercase letter occurs. For some reason i do not seem to be counting the uppercase occurrence properly. This is homework, any ideas? Thanks in advance.
package chapter_9;
public class Nine_Fifteen {
public static void main(String[] args) {
int caps = 0;
for(int i = 0;i < args.length;i++) {
if (Character.isUpperCase(args[0].codePointCount(i, i))){
caps++;
}
System.out.println("There are " + caps + " uppercase letters");
}
}
}
The problem is your use of
String.codePointCount:That’s not what you want – you’re passing that into
Character.isUpperCase, which isn’t right.Do you definitely need to handle non-BMP Unicode? If not, your code can be a lot simpler if you use
charAt(i)to get thecharat a particular index instead. You also want to loop from 0 toargs[0].length()as Kevin mentioned. I would suggest extracting theargs[0]part to a separate string variable to start with, to avoid the confusion:I won’t complete the code for you as it’s homework, but hopefully this will be enough to get you going.