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

Hello everyone i need help with a prime number program i'm getting a small error and don't know how to solve it
```// File: Prime.java                                                        |
// ------------------------------------------------------------------------+
// Author:  Shane Lowry                           Group: Group 1           |
// Student# B00059978                              Date: 13/11/2012        |
// ------------------------------------------------------------------------+
// Problem Set 2:  Problem 9                                               |
//                                                                         |
// DESCRIPTION:                                                            |
// Input a number and output if is a prime number is equal to 1.           |
//                                                                         |
//                                                                         |
// ------------------------------------------------------------------------+
import java.io.*;
import java.util.*;

public class Prime
{
public static void main(String[] args)throws IOException
{
// Set up an input buffer for reading input values
int i;

// Set up an output buffer for writing results to
PrintWriter output = new PrintWriter(System.out);

// Read in input values
String line1 = input.readLine();

// Convert string into binary
int x = Integer.parseInt(line1);

// Output input values
output.println("Prime Number: ");
for(i=1; i<x; i++){
int j;
for(j=2;j<i; j++){
int n = i%j;
if(n==0)
break;
}
}
if(i == j){
output.println(" "+i);}

// clean up output buffer
output.flush();
}
}```
Here's the error i keep getting
C:\Users\shane\Documents\Algorithem\Week 2 Problems\Prime.java:44: error: cannot find symbol
if(i == j){
^
symbol: variable j
location: class Prime
1 error

This is a scoping problem. The variable j is only visible within the scope of where it was declared. Thus it is only visible within the for loop as it was declared in there, and doesn't exist after the for loop ends (after the closing brace of the for loop).

can someone help me i'm trying to print out prime numbers starting with 1 but it doesn't print out 1 i know it maybe a stupid question but i can't seem to find out how to

If you want someone to suggest why the code you're using doesn't output 1, then you'll have to post the code you're using...

Also note that 1 isn't a prime number.

ok sorry here's the code and are sure about 1 not being a prime number
```// File: Prime.java                                                        |
// ------------------------------------------------------------------------+
// Author:  Shane Lowry                           Group: Group 1           |
// Student# B00059978                              Date: 13/11/2012        |
// ------------------------------------------------------------------------+
// Problem Set 2:  Problem 9                                               |
//                                                                         |
// DESCRIPTION:                                                            |
// Input a number and output if is a prime number is equal to 1.           |
//                                                                         |
//                                                                         |
// ------------------------------------------------------------------------+
import java.io.*;
import java.util.*;

public class Prime
{
public static void main(String[] args)throws IOException
{
// Set up an input buffer for reading input values

// Set up an output buffer for writing results to
PrintWriter output = new PrintWriter(System.out);

// Read in input values
String line1 = input.readLine();

// Convert string into binary
int X = Integer.parseInt(line1);
int i;

// Output input values
for(i=0;i<=X; i++){
int j;
for(j=2;j<i; j++){
int n = i%j;
if(n==0){
break;
}
}
if(i == j){
output.println(" "+i);}
}

// clean up output buffer
output.flush();
}
}```

```if(i == j){
output.println(" "+i);}
}```

We can be quite sure that won't print a "1" because j has just gone through a for loop where it was set to 2 and made to increment.

Prime number - Wikipedia, the free encyclopedia The first sentence should be enough. But there's a section on whether one is considered a prime.