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