Java Checking for Prime Digits in a Number

  • Thread starter Thread starter muna580
  • Start date Start date
  • Tags Tags
    Java String
AI Thread Summary
The discussion revolves around a programming error encountered while trying to check if every digit in a number is a prime number. The error messages indicate that a character cannot be dereferenced, specifically when using the .equals() method on a char type. It is clarified that since char is a primitive type, the correct approach is to use the equality operator (==) instead of .equals(). Additionally, suggestions are made to improve code style by eliminating unnecessary constants for prime digits and simplifying the logic in the loop. The recommended approach is to return false immediately if any digit is not prime, rather than setting a boolean variable to true for each prime digit. After implementing these changes, the program functions correctly.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top