Counting how many letters and numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • broception
    New Member
    • Feb 2013
    • 4

    Counting how many letters and numbers

    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.

    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");
    		
    	}
    
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Do the same thing, except for the characters '0' to '9'

    Comment

    Working...