Checking for Prime Digits in a Number

  • Context: Java 
  • Thread starter Thread starter muna580
  • Start date Start date
  • Tags Tags
    Java String
Click For Summary
SUMMARY

The forum discussion addresses a Java programming error encountered while checking if every digit in a number is a prime number. The error, "char cannot be dereferenced," arises from using the .equals() method on a char type in the PrimeNumbers.java code. The solution involves replacing the .equals() method with the '==' operator for character comparison. Additionally, it is recommended to simplify the code by removing unnecessary constants for prime digits and optimizing the loop structure.

PREREQUISITES
  • Java programming knowledge, specifically JDK version 1.5.0_08
  • Understanding of character data types and comparison in Java
  • Basic knowledge of prime numbers and their properties
  • Familiarity with control flow structures in Java, such as loops and conditionals
NEXT STEPS
  • Learn about Java character comparison using '==' versus .equals()
  • Study best practices for defining constants in Java programming
  • Explore efficient looping techniques in Java to enhance code readability
  • Investigate methods for validating prime numbers in Java
USEFUL FOR

Java developers, programming students, and anyone interested in improving their coding practices and debugging skills in Java.

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:
Technology 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.
 

Similar threads

Replies
18
Views
7K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 19 ·
Replies
19
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K