- #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: