Reputation: 31
import java.utils.Scanner;
public class PD {
public static void main(String[] args) {
System.out.print("Enter your number: " );
int number = input.nextInt();
System.out.println( DPPP(number) + "is double palidromic prime");
}
// main method checks every other method and returns a value that follows everything
public static int DPPP( int number) {
int count = 2;
while (count < number) {
String blank = "";
String Snumber = count + blank;
if (isPalindromic(count) && isPrime(count) &&
isPalindromic(Snumber.length()) && isPrime(Snumber.length()))
return count;
count++;
}
}
// method to find palindromic
public static boolean isPalindromic(int number) {
String blank = "";
String convert = count + blank;
for (int i = 0, int q = 1; i <= (convert.length()/2 - 1); i++, q++) {
if (convert.substring(i,q) == number.substring(number.length() - q, number.length() - i))
return true;
else
return false;
}
}
// method to find prime
public static boolean isPrime(int number) {
for (int divisor = 2; divisor <= number/2; divisor++) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
}
Error:
PD.java:28: <identifier> expected
for (int i = 0, int q = 1; i <= (convert.length()/2 - 1); i++, q++) {
^
PD.java:28: not a statement
for (int i = 0, int q = 1; i <= (convert.length()/2 - 1); i++, q++) {
^
PD.java:28: ')' expected
for (int i = 0, int q = 1; i <= (convert.length()/2 - 1); i++, q++) {
^
PD.java:28: ';' expected
for (int i = 0, int q = 1; i <= (convert.length()/2 - 1); i++, q++) {
^
PD.java:28: ';' expected
for (int i = 0, int q = 1; i <= (convert.length()/2 - 1); i++, q++) {
and I'm not sure why it ask to place a ; and why it says it's not a statement
Overall, I get that error and I don't understand why. thank you for the help! I know my program maybe incorrect, but I really want to focus on fixing this error. Once again, thank you for your help and time.
Upvotes: 1
Views: 94
Reputation: 1294
The issue appears to be the fact that you have two type identifiers in one statement (e.g. int i, int j;). When you declare two variables using the same statement, they are implicitly given the same type, so it should be changed to just:
for (int i = 0, q = 1; i <= (convert.length()/2 - 1); i++, q++) {
Another option is to calculate q from i within the loop:
for (int i = 0; i <= (convert.length()/2 - 1); i++) {
int q = i+1;
...
}
Upvotes: 2
Reputation: 835
I have a comment on your for loop, q is never considered in the stopping condition, therefore putting it within the loop is more logical in my opinion.
I changed your loop into this:
int q = 1;
for (int i = 0; i <= (convert.length()/2 - 1); i++) {
// code within the loop
q++;
}
Hope it helps you.
Upvotes: 0
Reputation: 209002
Try this
for (int i = 0,; i <= (convert.length()/2 - 1); i++) {
if (convert.substring(i,i + 1) == number.substring(number.length() - i + 1, number.length() - i))
Upvotes: 0