Hey, getting a new error in my code... Its almost done, cant figure out what is wrong with this.

ERROR:
non-static method isPrime(int) cannot be referenced from a static context

import java.util.Scanner;

public class primenumbertest {
		
	
    public boolean isPrime(int x){
    int divisor = 1;
   
   	do{
   		divisor += 1;
   	} while ((x % divisor) !=0);
}
   	
    public static void main(String[] args) {
    
    int num;
    boolean isItPrimeorNot;
    
    Scanner input = new Scanner(System.in);
    	
    System.out.println("Enter a number to find out if it's prime or not  (greater than 1): ");
 	num = input.nextInt();
 	
    input.close();
    
    isItPrimeorNot = isPrime(num);
    
    System.out.println(isItPrimeorNot);
    
    if (isItPrimeorNot == false) {
    	System.out.println(num + " is not a prime number.");
    }
    
    
    else { 
    	System.out.println(num + "is a prime number.");
    
    	}
    }
}

Recommended Answers

All 4 Replies

ERROR:
non-static method isPrime(int) cannot be referenced from a static context

As the error says, You cannot call a non-static method inside a static method.

So i cant say this in the main method? .....
isItPrimeorNot = isPrime(num);

Sorry im new to Java. (:

So i cant say this in the main method? .....
isItPrimeorNot = isPrime(num);

Sorry im new to Java. (:

You can make the method static. Like the main.

commented: Very helpful! +1

Ahhh Thanks that helped a lot. Fixed it. (:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.