I'm trying to count how many letters and how many numbers are in a user input string. For example:
Number01: should display
2 numbers
6 letters
So far, I've counted how many letters there are but I don't know how to add numbers to the string.
Number01: should display
2 numbers
6 letters
So far, I've counted how many letters there are but I don't know how to add numbers to the string.
Code:
import javax.swing.JOptionPane;
public class morePractice {
public static void main(String[] args) {
String str;
int num1;
int total=0;
char letter;
int totalNum=0;
str=JOptionPane.showInputDialog(null,"Enter phrase");
for(int i=0;i<str.length();i++){
letter=str.charAt(i);
if(letter>='A'&&letter<='Z' || letter>='a'&&letter<='z'){
total++;
}
}
JOptionPane.showMessageDialog(null,"There are " + total + " letters");
}
}
Comment