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: Need help people please help!!

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help people please help!!

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


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need help people please help!!

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

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help people please help!!

    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

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need help people please help!!

    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.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help people please help!!

    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
           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();
      }
    }

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need help people please help!!

    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.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help people please help!!

    so my code is right but do i have to set j = 1 ? please help im very confused

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need help people please help!!

    Try it and see. Better yet guess first: what do you think i%j will yield if j is 1? And how will that affect the numbers you report as prime.

    Personally I don't regard 1 as prime, but if you must, just print it before you start all the looping business.

Similar Threads

  1. Hi my java people!
    By Dotchild in forum Member Introductions
    Replies: 1
    Last Post: March 14th, 2012, 06:58 AM
  2. Hello Java People! :D
    By StandbyExplosion in forum Member Introductions
    Replies: 2
    Last Post: August 25th, 2011, 08:33 AM
  3. Hi people!
    By beer-in-box in forum Member Introductions
    Replies: 8
    Last Post: June 1st, 2011, 03:13 PM
  4. why do so few people use intellij idea?
    By hamsterofdeath in forum Java IDEs
    Replies: 4
    Last Post: August 5th, 2010, 01:28 AM
  5. Hello People
    By mrunal.cavale in forum Member Introductions
    Replies: 4
    Last Post: September 11th, 2009, 03:20 AM