# determining a prime number

• April 14th, 2013, 08:30 AM
ob7ivion
determining a prime number
Required:

Write a java program that asks a user to input a positive integer value. The program must keep on asking for an input if the user enters a non-positive integer value. Once a valid input is accepted, your program must output an appropriate message whether the value is a prime number or not.
Finally, the user will be asked if he/she would like to try again. It must be noted that the user will be repeatedly asked for an input until a valid response (pressing ‘Y’ or ‘y’ for YES; and ‘N’ or ‘n’ for NO) is entered.
Solve this problem using modular approach.
Code :

```import java.util.Scanner;   public class PrimeNumber { public static void main(String[] args) {   Scanner kbd = new Scanner(System.in); int num; boolean again;     do { num = inputPositiveInt(); if (isPrime(num)) { System.out.println("Input is a prime number."); } else { System.out.println("Input is a non-prime number."); }   // again = tryAgain(); } while (again); { System.out.println("Thank you for testing this program."); }   }     public static int inputPositiveInt() { int num; Scanner kbd = new Scanner(System.in); System.out.print("Please enter a positive integer."); num = kbd.nextInt(); if (num < 0) { System.out.println("Inputted integer value is non-positive"); } }   public static boolean tryAgain() { Scanner kbd = new Scanner(System.in); System.out.print("Would you like to try again?"); char again; again = kbd.charAt(0); if (again.equals('y') || again.equals('Y')) { return true; }   else if (again.equals('n') || again.equals('N')) { System.exit(0); } }   public static boolean isPrime(int num) { boolean isPrime = true;   for(int i = 2; i<num; i++) { if(num % i == 0) { isPrime = false; break; }     } return true; }   }```

ERRORS:
Code :

```F:\PrimeNumber.java:41: error: cannot find symbol again = kbd.charAt(0); ^ symbol: method charAt(int) location: variable kbd of type Scanner F:\PrimeNumber.java:42: error: char cannot be dereferenced if (again.equals('y') || again.equals('Y')) { ^ F:\PrimeNumber.java:42: error: char cannot be dereferenced if (again.equals('y') || again.equals('Y')) { ^ F:\PrimeNumber.java:46: error: char cannot be dereferenced else if (again.equals('n') || again.equals('N')) { ^ F:\PrimeNumber.java:46: error: char cannot be dereferenced else if (again.equals('n') || again.equals('N')) { ^```

i am simply frustrated because of getting errors like everywhere. been doin this for like 2hours or so.
i would appreciate it if someone could fix this for me. if possible.. ASAP. because i need it soon. thanks
• April 14th, 2013, 09:02 AM
Norm
Re: determining a prime number
Quote:

getting errors like everywhere
Please copy the full text of the error messages and paste it here.
• April 14th, 2013, 09:05 AM
ob7ivion
Re: determining a prime number
done editting.
• April 14th, 2013, 09:16 AM
Norm
Re: determining a prime number
Code :

```F:\PrimeNumber.java:41: error: cannot find symbol again = kbd.charAt(0); ^ symbol: method charAt(int) location: variable kbd of type Scanner```
The Scanner class does not have a method named: charAt()
kbd is a Scanner object.
You need to use a Scanner class method to read the user's input into a variable where the code can examine what was read.
Code :

```  F:\PrimeNumber.java:42: error: char cannot be dereferenced if (again.equals('y') || again.equals('Y')) { ^```
char variables are not objects and do not have methods like equals(). again is a char variable.
Use the == operator to compare char values.
• April 14th, 2013, 09:23 AM
ob7ivion
Re: determining a prime number
update
Code :

```import java.util.Scanner;   public class PrimeNumber { public static void main(String[] args) {   Scanner kbd = new Scanner(System.in); int num; boolean again;     do { num = inputPositiveInt(); if (isPrime(num)) { System.out.println("Input is a prime number."); } else { System.out.println("Input is a non-prime number."); }   // again = tryAgain(); } while (again); { System.out.println("Thank you for testing this program."); }   }     public static int inputPositiveInt() { int num; Scanner kbd = new Scanner(System.in); System.out.print("Please enter a positive integer."); num = kbd.nextInt(); if (num < 0) { System.out.println("Inputted integer value is non-positive"); } }   public static boolean tryAgain() { Scanner kbd = new Scanner(System.in); System.out.print("Would you like to try again?"); char again; again = kbd.nextInt(); if (again == 'y') || (again == 'Y') { return true; }   else if (again == 'n') || (again == 'N') { System.exit(0); } }   public static boolean isPrime(int num) { boolean isPrime = true;   for(int i = 2; i<num; i++) { if(num % i == 0) { isPrime = false; break; }     } return true; }   }```

errors:
Code :

```F:\PrimeNumber.java:42: error: illegal start of expression if (again == 'y') || (again == 'Y') { ^ F:\PrimeNumber.java:42: error: ';' expected if (again == 'y') || (again == 'Y') { ^ F:\PrimeNumber.java:46: error: 'else' without 'if' else if (again == 'n') || (again == 'N') { ^ F:\PrimeNumber.java:46: error: illegal start of expression else if (again == 'n') || (again == 'N') { ^ F:\PrimeNumber.java:46: error: ';' expected else if (again == 'n') || (again == 'N') {```
• April 14th, 2013, 09:25 AM
Norm
Re: determining a prime number
The if statement's condition need to be enclosed in ()s. The () must surround ALL of the sub conditions.
• April 14th, 2013, 09:30 AM
ob7ivion
Re: determining a prime number
Quote:

Originally Posted by Norm
The if statement's condition need to be enclosed in ()s. The () must surround ALL of the sub conditions.

fixed. lol.

Code :

```import java.util.Scanner;   public class PrimeNumber { public static void main(String[] args) {   Scanner kbd = new Scanner(System.in); int num; boolean again;     do { num = inputPositiveInt(); if (isPrime(num)) { System.out.println("Input is a prime number."); } else { System.out.println("Input is a non-prime number."); }   // again = tryAgain(); } while (again); { System.out.println("Thank you for testing this program."); }   }     public static int inputPositiveInt() { int num; Scanner kbd = new Scanner(System.in); System.out.print("Please enter a positive integer."); num = kbd.nextInt(); if (num < 0) { System.out.println("Inputted integer value is non-positive"); } }   public static boolean tryAgain() { Scanner kbd = new Scanner(System.in); System.out.print("Would you like to try again?"); char again; again = kbd.nextLine(); if (again == 'y' || again == 'Y') { return true; }   else if (again == 'n' || again == 'N') { System.exit(0); } }   public static boolean isPrime(int num) { boolean isPrime = true;   for(int i = 2; i<num; i++) { if(num % i == 0) { isPrime = false; break; }     } return true; }   }```

and another error..
Code :

```F:\PrimeNumber.java:41: error: incompatible types again = kbd.nextLine(); ^ required: char found: String 1 error```
• April 14th, 2013, 10:27 AM
Norm
Re: determining a prime number
Quote:

required: char
found: String
The variable on the left of the = is type char.
The value on the right of the = is a String.
Those types are not compatible for an assignment.
Change the variable on the left to a String.