Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Wednesday, January 21, 2009

Count Lowercase, Digits and Uppercase Letter from String

import java.io.BufferedReader;

import java.io.InputStreamReader;

/**

* Accept a sentence from the command line containing a mixture of lowercase,

* digits and uppercase letters, Display a count of each.

*/

public class CheckNumberUpperCase

{

/**

* gets the input from command line and count the occurence of

* lowercase, uppercase letter and digit.

* @throws Exception

*/

public static void countAlphabetsNos() throws Exception

{

// get the input string from user..

BufferedReader brConsole=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Any String (mixture of digits & alphabets) : ");

String strInput=brConsole.readLine();

// variable for counting occurence

int intLowerCnt=0,intUpperCnt=0,intDigitCnt=0,intOtherCnt=0;

/// check length of input

if(strInput==null || strInput.length()<=0)

System.out.println(strInput + " Is Invalid Input!!!");

else

{

// scan each character and increment the counter

for(int i=0;i<strInput.length();i++)

{

int no=(char)strInput.charAt(i);

// upper case

if((no>=65 && no<=90))

intUpperCnt++;

else if(no>=97 && no<=122) // lower case

intLowerCnt++;

else if(no>=48&& no<=57) // digit

{

intDigitCnt++;

}

else

intOtherCnt++;

}

System.out.println("lowerCnt=" + intLowerCnt + "\nupperCnt=" + intUpperCnt + "\ndigitCnt=" + intDigitCnt + "\notherCnt=" + intOtherCnt);

}

}

/**

* excecution begin from here

* @param args

*/

public static void main(String[] args)

{

try

{

countAlphabetsNos();

}

catch(Exception e)

{

System.out.println("Error occured!!");

}

}

}

No comments:

Post a Comment

Followers