Checking for Prime Digits in a Number

  • Context: Java 
  • Thread starter Thread starter muna580
  • Start date Start date
  • Tags Tags
    Java String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 40K views
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:
Physics news on Phys.org
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:
Oh, I see, thanks very much. I removed the equals and replaced it with == and it worked.