Checking for Prime Digits in a Number

  • Java
  • Thread starter muna580
  • Start date
  • Tags
    Java String
In summary, the conversation discusses a program that checks if every digit in a number is a prime number. The person executing the program encountered an error due to using .equals() on a char, which is not an object. The expert also provides suggestions for improving the code style, such as using == instead of .equals() and simplifying the loop.
  • #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:
Technology news on Phys.org
  • #2
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
Oh, I see, thanks very much. I removed the equals and replaced it with == and it worked.
 

1. What is the difference between int, String, and char in Java?

In Java, int is a primitive data type that represents integer values, while String is a class that represents a sequence of characters. Char, on the other hand, is also a primitive data type that represents a single character. Int is used for numeric calculations, String is used for storing text, and char is used for single character values.

2. How do I convert an int to a String in Java?

To convert an int to a String in Java, you can use the toString() method. For example, if x is an integer variable, you can use the code snippet String xStr = Integer.toString(x); This will convert the int value to a String and store it in the variable xStr.

3. How do I convert a String to a char in Java?

To convert a String to a char in Java, you can use the charAt() method. This method takes in an index as a parameter and returns the character at that index in the String. For example, if str is a String variable, you can use the code snippet char c = str.charAt(0); This will retrieve the first character in the String and store it in the variable c.

4. Can I convert a char to an int in Java?

Yes, you can convert a char to an int in Java. This can be done by using the char's ASCII value. For example, if c is a char variable, you can use the code snippet int ascii = (int) c; This will convert the char value to its corresponding ASCII value and store it in the integer variable ascii.

5. How do I convert a char to a String in Java?

To convert a char to a String in Java, you can use the valueOf() method. This method takes in a char as a parameter and returns a String representation of the char. For example, if c is a char variable, you can use the code snippet String cStr = String.valueOf(c); This will convert the char value to a String and store it in the variable cStr.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
14
Views
7K
  • Programming and Computer Science
Replies
19
Views
4K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
18
Views
6K
Back
Top