Hi guys - I need to write a program that reads in a user-input integer and tells if the number is prime or not. I've written up this code so far:

class primenumber
{
	public static void main(String[] args){
	int num = 10;
	int i;
		
	
	for (i=2;  i < num ;i++){
	int n = num%i;
	if (n==0){
	System.out.println("Your number is not a prime number.");
	break;
	}
}
if (i == num){
System.out.println("Your number is a prime number!");
}
}
}

I can tell if a number is prime or not if I assign num in the code, but I am having some trouble/confusion integrating this with prompting the user to enter an integer. Any help would be much appreciated.

prompting the user to enter an integer

Look at the Scanner class for an easy way to read input from a user via the console.
Basically you print out a request, read in the user's response, check that it is a valid number and then test if it's prime.

Hi guys - I need to write a program that reads in a user-input integer and tells if the number is prime or not. I've written up this code so far:

class primenumber
{
	public static void main(String[] args){
	int num = 10;
	int i;
		
	
	for (i=2;  i < num ;i++){
	int n = num%i;
	if (n==0){
	System.out.println("Your number is not a prime number.");
	break;
	}
}
if (i == num){
System.out.println("Your number is a prime number!");
}
}
}

I can tell if a number is prime or not if I assign num in the code, but I am having some trouble/confusion integrating this with prompting the user to enter an integer. Any help would be much appreciated.

Check here for some help:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Here is my updated code, I have thanks for the link and the suggestion about Scanner classes. My only other question is: Optionally, we can use an else statement to tell the user they entered an invalid integer if their number is less than two. Coming from a Python class previously, I am a little confused as to how to incorporate the else statement into the code.

I imagine something like:

else (num < 2){
System.out.println("Invalid integer.");
}

However, when I try to place this in the code, it acts as if it does not exist. User-input for a number less than 2 returns nothing.

import java.util.Scanner;
class primenumber
{
	public static void main(String[] args){
	Scanner keyboard = new Scanner(System.in);
	System.out.println("Please enter a number:");
	int num = keyboard.nextInt();
	int i;
		
	
	for (i=2;  i < num ;i++){
	int n = num%i;
	if (n==0){
	System.out.println("Your number is not a prime number.");
	break;
	}
}
if (i == num){
System.out.println("Your number is a prime number!");
}
else (num < 2){
System.out.println("Invalid interger.");
}
}
}

The code you have posted has compiler errors.
Are you able to compile it without errors?

if(condition){
}
else{
}

is valid but this isnt :

if(condition){
}
else(condition){
}

try using this instead :

if(condition){
}
else if (condition){
}
//optionaly add :
else{
}

little extra comment :

i prefer declaring my loop variables inside the for statement to prevent confusion between multiple loops in the same scope, it restrains the scope of the loop variable to the loop.

int i;//not declared here (personal preference, still works this way)
for (i=2;  i < num ;i++){
int n = num%i; //declaring a variable inside a loop isn't optimal, declare it before
...
}

like so :

int n;
for (int i = 2 ; i < num ; i++){
n = num%i;
...
}

Thanks for all the help everyone.

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.