Finding the uppercase and lowercase characters in a string

input string, output upper/lowercase character

Page 1 of 1

6 Replies - 22072 Views - Last Post: 28 September 2009 - 07:41 PM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 03:15 PM

I wrote this program, and I am having problems with it. In my countLetters method, I figure that no one will enter a line over 50 characters. This is what I have so far, but I can't get past these errors....

import java.io.*;
import java.util.*;

public class UppercaseLetters
{
	public static void main(String[] args)
	{
		Scanner console = new Scanner(System.in);
		String userinput;
		
		System.out.print("Enter a line of letters. I will then tell you how many of them are upper case, and how ");
		System.out.print("many of them\nare lower case.");	
		
		System.out.println("\n");
		System.out.print("Enter your line of letters: ");
			
			userinput = console.nextLine();
			
		int[] l = countLetters(userinput.toLowerCase());
		int[]	u = countLetters(userinput.toUpperCase());	
		
		for (int i = 0; i < l.length; i++)
			System.out.println("You have " + l[i] + " lower case characters.");
		
		for (int k = 0; k < u.length; k++)
			System.out.println("You have " + u[k] + "	upper case characters.");
		
	}
	
	public static int[] countLetters(String userinput)
	{
		int[] l = new int[50];
		int[] u = new int[50];
		
		for (int i = 0; i < userinput.length(); i++)
			{
				if (Character.toLowerCase(userinput))
					l++;
					
				else 
				{
					u++;
				}	
			}		
		
		return l;
		return u;
	}		
}	


AND, these are the errors the compiler throws....

----jGRASP exec: javac -g C:\Users\Christopher\Documents\UppercaseLetters.java

UppercaseLetters.java:43: cannot find symbol
symbol : method toLowerCase(java.lang.String)
location: class java.lang.Character
if (Character.toLowerCase(userinput))
^
UppercaseLetters.java:44: operator ++ cannot be applied to int[]
l++;
^
UppercaseLetters.java:48: operator ++ cannot be applied to int[]
u++;
^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Can someone help me sort this out??? TY in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Finding the uppercase and lowercase characters in a string

#2 Neosmithk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 83
  • Joined: 24-June 09

Re: Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 03:42 PM

if you want to iterate through an array you cannot iterate the actual array you must do this
for(int i = 0; i<array.length; i++)
{
l[i] = someothervariable;
}

second toUpperCase() and toLowerCase() are functions that return the input string in all caps or all lowercase respectively

what you want is a function that tells you weather or not a letter is uppercase or lower case and then iterate a regular integer, not a int array.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,091
  • Joined: 27-December 08

Re: Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 04:00 PM

Use the Character.isLowerCase(char c) and the Character.isUpperCase(char c) methods like so:

String x;
int uppercase = 0, lowercase = 0;
for(int i = 0; i < x.length(); i++){
   if(Character.isUpperCase(x.charAt(i))) uppercase++;
   else if(Character.isLowerCase(x.charAt(i))) lowercase++;
}



Remember, since numbers, punctuation marks, whitespaces, and linebreaks are all characters, you cannot use an else statement by itself after the first if statement.
Was This Post Helpful? 1
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 06:04 PM

View Postmacosxnerd101, on 28 Sep, 2009 - 03:00 PM, said:

Use the Character.isLowerCase(char c) and the Character.isUpperCase(char c) methods like so:

String x;
int uppercase = 0, lowercase = 0;
for(int i = 0; i < x.length(); i++){
   if(Character.isUpperCase(x.charAt(i))) uppercase++;
   else if(Character.isLowerCase(x.charAt(i))) lowercase++;
}



Remember, since numbers, punctuation marks, whitespaces, and linebreaks are all characters, you cannot use an else statement by itself after the first if statement.


Ok, so these are my changes....
import java.io.*;
import java.util.*;

public class UppercaseLetters
{
	public static void main(String[] args)
	{
		Scanner console = new Scanner(System.in);
		String userinput;
		
		System.out.print("Enter a line of letters. I will then tell you how many of them are upper case, and how ");
		System.out.print("many of them\nare lower case.");	
		
		System.out.println("\n");
		System.out.print("Enter your line of letters: ");
			
			userinput = console.nextLine();
			
		int[] l = countLetters(userinput.toLowerCase());
		int[]	u = countLetters(userinput.toUpperCase());	
		
		for (int i = 0; i < l.length; i++)
			System.out.println("You have " + l[i] + " lower case characters.");
		
		for (int k = 0; k < u.length; k++)
			System.out.println("You have " + u[k] + "	upper case characters.");
		
	}
	
	public static int[] countLetters(String userinput)
	{
		int u = 0;
		int l = 0;
		
		for (int i = 0; i < userinput.length(); i++)
			{
				if (Character.isLowerCase(userinput.charAt(i)))
				{
					l++;
					return l;
				}
					
				else if (Character.isUpperCase(userinput.charAt(i)))
				{
					u++;
					return u;
				}		
	
			}	
	}			
}


And this is what the compilers throws...

----jGRASP exec: javac -g C:\Users\Christopher\Documents\UppercaseLetters.java

UppercaseLetters.java:46: incompatible types
found : int
required: int[]
return l;
^
UppercaseLetters.java:52: incompatible types
found : int
required: int[]
return u;
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 06:10 PM

you do not need these array

int[] l = countLetters(userinput.toLowerCase());
int[] u = countLetters(userinput.toUpperCase());

you do not need an array of the number of letter to count just nbLower and nbUpper
also if you change the case of your inut String using toLowerCase() and toUpperCase() sure that all your letters will be lower or upper case

review a bit your logic
Was This Post Helpful? 0
  • +
  • -

#6 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 06:46 PM

View Postpbl, on 28 Sep, 2009 - 05:10 PM, said:

you do not need these array

int[] l = countLetters(userinput.toLowerCase());
int[] u = countLetters(userinput.toUpperCase());

you do not need an array of the number of letter to count just nbLower and nbUpper
also if you change the case of your inut String using toLowerCase() and toUpperCase() sure that all your letters will be lower or upper case

review a bit your logic


I am not trying to turn all of them into upper or lower, I made a little mistake, is was supposed to be isUpper or isLower, I need to count the Upper case characters and the lower case characters.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Finding the uppercase and lowercase characters in a string

Posted 28 September 2009 - 07:41 PM

import java.io.*;
import java.util.*;

public class UppercaseLetters
{
	public static void main(String[] args)
	{
		Scanner console = new Scanner(System.in);
		String userinput;

		System.out.print("Enter a line of letters. I will then tell you how many of them are upper case, and how ");
		System.out.println("many of them\nare lower case.");	

		System.out.print("Enter your line of letters: ");

		userinput = console.nextLine();

		int[] c = countLetters(userinput);
		System.out.println("You have " + c[0] + " lower case characters.");
		System.out.println("You have " + c[1] + "	upper case characters.");

	}

	public static int[] countLetters(String userinput)
	{
		int[] c = new int[2];

		for (int i = 0; i < userinput.length(); i++)
		{
			if (Character.isLowerCase(userinput.charAt(i)))
			{
				c[0]++;
			}

			else if (Character.isUpperCase(userinput.charAt(i)))
			{
				c[1]++;
			}		

		}  
		return c;
	}			
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1