Java: Int to String to Char

  • Java
  • Thread starter muna580
  • Start date
  • #1
muna580
How come when I exectue this program, I get the follwing error. Just to let you know, what i am trying to do is, check if ever digit in a number is a prime number.

--------------------Configuration: Number21 - JDK version 1.5.0_08 <Default> - <Default>--------------------
C:\Documents and Settings\Owner\My Documents\School Stuff\Clubs\Number21\PrimeNumbers.java:28: char cannot be dereferenced
if((valueStr.charAt(i)).equals(prime2) || (valueStr.charAt(i)).equals(prime3) ||
^
C:\Documents and Settings\Owner\My Documents\School Stuff\Clubs\Number21\PrimeNumbers.java:28: char cannot be dereferenced
if((valueStr.charAt(i)).equals(prime2) || (valueStr.charAt(i)).equals(prime3) ||
^
C:\Documents and Settings\Owner\My Documents\School Stuff\Clubs\Number21\PrimeNumbers.java:29: char cannot be dereferenced
(valueStr.charAt(i)).equals(prime5) || (valueStr.charAt(i)).equals(prime7))
^
C:\Documents and Settings\Owner\My Documents\School Stuff\Clubs\Number21\PrimeNumbers.java:29: char cannot be dereferenced
(valueStr.charAt(i)).equals(prime5) || (valueStr.charAt(i)).equals(prime7))
^
4 errors

Process completed.
Code:
import java.lang.Math; 
 
public class PrimeNumbers
{
	public static void main (String[] args)
	{
		int[] test = {546513, 77777, 24756, 2226};
		
		for(int i = 0; i<test.length; i++)
		{
			System.out.println ("Integer " + test[i] + " is a prime " + arePrime(test[i]));
		}
	}
	
	public static boolean arePrime(int value)
    {
    	char prime2 = '2'; 
    	char prime3 = '3';
    	char prime5 = '5';
    	char prime7 = '7';
    	
    	boolean arePrimeDigits = false;
    	
    	String valueStr = String.valueOf(value);
    	
    	for (int i = 0; i<valueStr.length(); i++)
    	{
    		if((valueStr.charAt(i)).equals(prime2) || (valueStr.charAt(i)).equals(prime3) || 
    		(valueStr.charAt(i)).equals(prime5) || (valueStr.charAt(i)).equals(prime7))
    		{
    			arePrimeDigits = true;
    		}
    		else 
    		{
    			arePrimeDigits = false;
				i = valueStr.length();
    		}
    	}
    	
    	return arePrimeDigits; 
    }
}
 
Last edited by a moderator:

Answers and Replies

  • #2
0rthodontist
Science Advisor
1,230
0
A char is not an object, so you can't ask if it .equals() anything. Just use ==.

Also 2 notes on your code style: First, there's no need to define constants prime2, prime3, etc. It's good to define constants for anything that might change in the future, but the primes are not going to change. Second, your loop is a bit clumsy. Don't set a variable to true every time--just return false if any digit is not prime. You might write it like

Code:
    	for (int i = 0; i<valueStr.length(); i++)
    	{
    		if any digit is not prime
    			return false;
	}
return true;
 
Last edited:
  • #3
muna580
Oh, I see, thanks very much. I removed the equals and replaced it with == and it worked.
 

Related Threads on Java: Int to String to Char

  • Last Post
Replies
3
Views
6K
Replies
5
Views
1K
Replies
2
Views
1K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
7
Views
6K
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
7
Views
2K
Replies
10
Views
2K
  • Last Post
Replies
1
Views
1K
Top