vendredi 20 février 2015

it compiles but gives me an exception



the program is suppose to intake sentence and return the number of uppercase letter, lower case , and digits. it compiles asks for input and returns:



Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.charAt(Unknown Source)
at projects.projectzero.check(projectzero.java:16)
at projects.projectzero.main(projectzero.java:10)





package projects;

import javax.swing.JOptionPane;

public class projectzero {
public static void main(String args[]) {
String sentence = "", addToSentence = "";
while (!addToSentence.equalsIgnoreCase("stop")) {
addToSentence = JOptionPane.showInputDialog("input words or enter stop to end program");
sentence += addToSentence;
}
check(sentence);
}

public static void check(String input) {
int upperCase = 0, lowerCase = 0, numbers = 0;
for (int i = 0; i <= input.length(); i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c))
upperCase++;
else if (Character.isLowerCase(c))
lowerCase++;
else if (Character.isDigit(c)) numbers++;
}
System.out.println("the number of uppercase: " + upperCase + " the number of lower case : " + lowerCase + "the number of digits is: "
+ numbers);
}
}




appreciate the help :)




Aucun commentaire:

Enregistrer un commentaire