Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: determining a prime number

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: determining a prime number

    getting errors like everywhere
    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: determining a prime number

    done editting.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: determining a prime number

    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.
     
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: determining a prime number

    update
    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:
    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') {

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: determining a prime number

    The if statement's condition need to be enclosed in ()s. The () must surround ALL of the sub conditions.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: determining a prime number

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

    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..
    F:\PrimeNumber.java:41: error: incompatible types
            again = kbd.nextLine();
                                ^
      required: char
      found:    String
    1 error

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: determining a prime number

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help determining if number is a palindrome using an array!
    By jetset222 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2013, 10:09 PM
  2. Prime number solver.
    By Danny123 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: June 4th, 2012, 05:30 AM
  3. display all prime factors of a number
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2012, 06:55 PM
  4. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM
  5. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM