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.

Page 2 of 2 FirstFirst 12
Results 26 to 43 of 43

Thread: Prime number problem (please help)

  1. #26
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Please post the whole class where that statement is.

    The error message says that statement is in the wrong place in the code.

    Do you know how to define a variable?
    i posted the entire program a while ago. See post #20.

    a variable is a temporary data store. it is defined using a specified data type for example int,double,char,string followed by the variable name.
    for example int num;

    I dont understand what you are telling me to do. My tutor said he is not teaching files but will be tested on it.
    Can anyone on this forum help me please?!

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    i posted the entire program a while ago. See post #20.
    It has a problem and needs to be changed. That is what all the posts since then have been about. You were supposed to change the code.

    Move the definition for the variable: out outside of the main() method so it is a class instance variable, NOT a variable that is locally defined in the main() method. Then all the methods in the class will be able to use it.

    Do some research on what the scope of a variable means.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    It has a problem and needs to be changed. That is what all the posts since then have been about. You were supposed to change the code.

    Move the definition for the variable: out outside of the main() method so it is a class instance variable, NOT a variable that is locally defined in the main() method. Then all the methods in the class will be able to use it.

    Do some research on what the scope of a variable means.
    man this is not working. i did what u just said and this is what i get

    --------------------Configuration: <Default>--------------------
    C:\Users\DesktopPrimes.java:13: error: non-static variable out cannot be referenced from a static context
    out.close();
    ^
    C:\Users\DesktopPrimes.java:26: error: non-static variable out cannot be referenced from a static context
    out.printf(i);
    ^
    C:\Users\DesktopPrimes.java:26: error: no suitable method found for printf(int)
    out.printf(i);
    ^
    method PrintWriter.printf(Locale,String,Object...) is not applicable
    (actual argument int cannot be converted to Locale by method invocation conversion)
    method PrintWriter.printf(String,Object...) is not applicable
    (actual argument int cannot be converted to String by method invocation conversion)
    3 errors

    Process completed.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    non-static variable out cannot be referenced from a static context
    Too much code in static methods is causing that error.
    Did you Google that error. It is very common.

    For now make out a static variable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Too much code in static methods is causing that error.
    Did you Google that error. It is very common.

    For now make out a static variable.
    what variable do i make a static variable for???

  6. #31
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Prime number problem (please help)

    The error says:

    C:\Users\DesktopPrimes.java:13: error: non-static variable out cannot be referenced from a static context
    out.close();
    ^

    Notice there's a little caret that even points at it for you.

    Also notice that your last error indicates that there is no method printf( int ) for the variable out.

  7. #32
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by GregBrannon View Post
    The error says:

    C:\Users\DesktopPrimes.java:13: error: non-static variable out cannot be referenced from a static context
    out.close();
    ^

    Notice there's a little caret that even points at it for you.

    Also notice that your last error indicates that there is no method printf( int ) for the variable out.
    ok i solved the printf(int) error. Now how do i fix the out.printf???

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    Is this the one?
    no suitable method found for printf(int)
    Change printf() to print()
    Or read the API doc for the printf() method to see how to use it.

    If you have questions about the API doc, copy the doc here and ask your questions about the part you are confused about.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Is this the one?

    Change printf() to print()
    Or read the API doc for the printf() method to see how to use it.

    If you have questions about the API doc, copy the doc here and ask your questions about the part you are confused about.
    i did that but now im stil getting the same error at the out.close() and out.print.

    this is the error:error: non-static variable out cannot be referenced from a static context

    i googled it but i dot understand what to do.

    this is due today

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    Make out a static variable.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Make out a static variable.
    ok but what variable do i use for that?

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    ??? I just said the name of the variable that should be made static: out

    See the tutorial about static: http://docs.oracle.com/javase/tutori...variables.html
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    ??? I just said the name of the variable that should be made static: out

    See the tutorial about static: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
    can u just tell me how to do it?
    i dont understand what you are saying.
    out is used as my file pointer for the output file.

    PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
    how do i fix this?

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    Put the keyword static first thing on the line.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #40
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Put the keyword static first thing on the line.
    Static PrintWriter out = new PrintWriter(new FileWriter("output.txt"));

    is that it?

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    Did you try changing the source and compiling it? What did the compiler say?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Did you try changing the source and compiling it? What did the compiler say?
    I dont know what you mean by changing the source. I tried using the static thing like you said but its giving an error saying cannot be dereferenced.

    So i took out the static thing altogether. What i did is moved the PrintWriter out statement and the out.close() statement to the Sieve method and it works perfectly now. The primes are outputted to a file.

    Although i did not understand too much of what u said I guess it helped a bit. So thanks!

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Prime number problem (please help)

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    javaman1 (June 21st, 2014)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Check if number is prime number or not problem[SOLVED]
    By kinkita in forum Loops & Control Statements
    Replies: 9
    Last Post: July 17th, 2013, 10:59 AM
  2. determining a prime number
    By ob7ivion in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2013, 09:27 AM
  3. Problem with my prime number finder program
    By Truck35 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 11:25 PM
  4. Prime number solver.
    By Danny123 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: June 4th, 2012, 05:30 AM
  5. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM