By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

Counting how many letters and numbers

P: 4
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.

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class morePractice {
  4.  
  5.     public static void main(String[] args) {
  6.         String str;
  7.         int num1;
  8.         int total=0;
  9.         char letter;
  10.         int totalNum=0;
  11.  
  12.         str=JOptionPane.showInputDialog(null,"Enter phrase");
  13.  
  14.         for(int i=0;i<str.length();i++){
  15.             letter=str.charAt(i);
  16.             if(letter>='A'&&letter<='Z' || letter>='a'&&letter<='z'){
  17.                 total++;
  18.             }
  19.  
  20.         }
  21.         JOptionPane.showMessageDialog(null,"There are " + total + " letters");
  22.  
  23.     }
  24.  
  25. }
  26.  
Mar 5 '13 #1

✓ answered by Rabbit

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

Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,421
Do the same thing, except for the characters '0' to '9'
Mar 5 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.