d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How Do I Detect The Number Of Lower/upper Case?
Add Reply New Topic New Poll
Member
Posts: 54,832
Joined: Jan 10 2009
Gold: 1,992.00
Sep 28 2015 02:36am
I know how to convert lower to upper and vice versa, but how can I detect how many upper and lower cases there are in java?

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 54,832
Joined: Jan 10 2009
Gold: 1,992.00
Sep 28 2015 11:20am
I'm not too familiar with java, but I learned basic C++. How would I be able to find how many of a certain letter is either uppercase or lower case in a sentence?

For example the number of "a" and "A" in a string.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 28 2015 11:59am
Iterate through each char and call Character.isUpperCase() to determine if it's uppercase

Code
String test = "Some String";
int upperCaseCount = 0;
for ( char c : test.toCharArray( ) ) {
if ( Character.isUpperCase( c ) ) {
upperCaseCount++;
}
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2015 04:46pm
If you want to count the number of occurences of each character of the alphabet I would recommend using an integer array of length 26.
Member
Posts: 54,832
Joined: Jan 10 2009
Gold: 1,992.00
Sep 28 2015 05:44pm
Quote (Minkomonster @ Sep 28 2015 05:46pm)
If you want to count the number of occurences of each character of the alphabet I would recommend using an integer array of length 26.


How would I do this? I need to find the number of uppercase I and lowercase i from a sentence input by the user.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2015 07:24pm
Quote (Whalefood @ Sep 28 2015 06:44pm)
How would I do this? I need to find the number of uppercase I and lowercase i from a sentence input by the user.


Then it is even simpler.

Using the code labatymo provided you can modify it to do what you need:

Code
String test = "Some String";
int upperCaseCount = 0;
int lowerCaseCount = 0;

for ( char c : test.toCharArray( ) ) {
if ( c == 'I') {
upperCaseCount++;
}
else if(c == 'i'){
lowerCaseCount++;
}
}
Member
Posts: 54,832
Joined: Jan 10 2009
Gold: 1,992.00
Sep 28 2015 08:04pm
I'm supposed to use charAt method. How can I apply this instead of char c :test.
Member
Posts: 54,832
Joined: Jan 10 2009
Gold: 1,992.00
Sep 28 2015 09:12pm
Nevermind I got it fixed. I just arranged my lines weird.

Thanks for the help guys

This post was edited by Whalefood on Sep 28 2015 09:33pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 28 2015 09:34pm
Quote (Whalefood @ Sep 28 2015 09:04pm)
I'm supposed to use charAt method. How can I apply this instead of char c :test.


Code
for(int i = 0; i < s.length(); i++)
if(s.charAt(i) == 'I')
//do something



Apply yourself, kid. Or just give up. Its 2015. You can google your way to any degree you can imagine. Here, let me show you:
http://lmgtfy.com/?q=java+count+uppercase+letters
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll