• November 17th, 2012, 04:46 PM
shep
Hello everyone i need help with a prime number program i'm getting a small error and don't know how to solve it
Code Java:

```// 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 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 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
• November 17th, 2012, 06:35 PM
curmudgeon
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).
• November 18th, 2012, 06:18 AM
shep
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
• November 18th, 2012, 06:39 AM
pbrockway2
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.
• November 18th, 2012, 04:16 PM
shep
ok sorry here's the code and are sure about 1 not being a prime number
Code :

```// 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 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));   // 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(); } }```
• November 18th, 2012, 04:30 PM
pbrockway2
Code :

```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.
• November 18th, 2012, 05:28 PM
shep