I am writing a java program. One of the functions of my program is to count all the uppercase letters in a paragraph of text I import. I am not sure how to set this up.
Is it something like,

isUpperCase.(char)

Any help would be great

Thanks

Maybe something like this would work, but I'm not sure:

String text = textArea.getText();
int upperCaseCount = 0;

for (int i=0; i<text.length(); i++)
{
     for(char c='A'; c<='Z'; c++)
    {
           if (text.charAt(i) == c)
          {
                    upperCaseCount++;
          }
     }
}

Here's a real example of it:

class UCase
{
  public static void main(String[] args)
  {
	String s = "This has Three Upper case characters";
	int upperCaseCount = 0;

	for (int i=0; i<s.length(); i++)
	{
    		 for(char c='A'; c<='Z'; c++)
    		{
        		   if (s.charAt(i) == c)
          		   {
                    		upperCaseCount++;
          		   }		
     		}
	}
	System.out.println(upperCaseCount + "");
  }
}

you can look at the values of the character, (for ASCII code table)
for example value of 'A' is 65 and value of 'Z' is 90
so you can check every character in your string, then check if characters are between 65 and 90, if true then increase counter, if false do nothing.

I'd expect a regular expression that extracts all uppercase letters into a new String, followed by simply taking the length of that String, to be the shortest code.
The regular expression could get messy though, and not being an expert at them I'm not going to try writing one :)

I'd expect a regular expression that extracts all uppercase letters into a new String, followed by simply taking the length of that String, to be the shortest code.
The regular expression could get messy though, and not being an expert at them I'm not going to try writing one :)

You could use the same thing I gave and still be able to easily extract them and put the UCase characters in a new string.

you can look at the values of the character, (for ASCII code table)
for example value of 'A' is 65 and value of 'Z' is 90
so you can check every character in your string, then check if characters are between 65 and 90, if true then increase counter, if false do nothing.

Exactly what I did, except I made it much easier.

well your code is a bit slower, because in every character your program compare with the whole alphabet, but mine only with two number,
so in a string with 5 character,
yours, 5*26 comparison,
mine, only 5*2

accually when string is very long, both algorithm's time gets closer... both algorithm's speed is O(n) where n is the length of the string.

well your code is a bit slower, because in every character your program compare with the whole alphabet, but mine only with two number,
so in a string with 5 character,
yours, 5*26 comparison,
mine, only 5*2

accually when string is very long, both algorithm's time gets closer... both algorithm's speed is O(n) where n is the length of the string.

I compare only with uppercase character just as you do. Not meaning to be rude here, but how do you get 5 *2? I mean, don't you have to compare each character with all the ASCII codes pertaining to u case char's?

Also, how would you do this? would it be something like this:

for (int i=0; i<text.length(); i++)
{
   int charPoint = (char)text.charAt(i);
  if (charPoint >= 65 && charPoint <= 90)
  {
     //its ucase
  }
}

After writing that it doesn't seem like a bad solution at all.

Or, you could simply check if the character is upper case.

public static void main()
	{
		String s = "This has Three Upper case characters";
		int caps = 0;
		
		for (int i=0; i<s.length(); i++)
		{
			if (Character.isUpperCase(s.charAt(i)))
				caps++;
		}
		
		System.out.println(caps);
		
	}

Or, you could simply check if the character is upper case.

public static void main()
	{
		String s = "This has Three Upper case characters";
		int caps = 0;
		
		for (int i=0; i<s.length(); i++)
		{
			if (Character.isUpperCase(s.charAt(i)))
				caps++;
		}
		
		System.out.println(caps);
		
	}

Even better, good job!

This article has been dead for over six months. Start a new discussion instead.