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

Thread: strange problem

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry strange problem

    hey doods
    i am begginer and i am trying to write a programe which print the first 100 primers numbers.
    here is the code - and i don't understand why its not even run...
    *****there is a Terminal class which is special but dont mind it is just an input/output class....the bug not that way...
    i am realy need help and i don't want to look for programe already done on the net
    thank u very much

    public class Premiers{
    public static void main(String[]args){
    int n=2;
    boolean p=true;
    for(int i=1;i<=100;i=i+1){
    for(int d=1;d<n;d=d+1){
    if(n%d==0){
    p=false;
    }
    }
    if(p!=false){
    Terminal.ecrireStringln(n+" est un n.primer");
    }
    n=n+1;
    }
    }
    }


  2. #2
    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: strange problem

    why its not even run
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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: strange problem

    Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags and other useful info for newcomers.

    First, simplify what you have to an algorithm that can simply find prime numbers. Start with finding all prime numbers between zero and 100. Come back when you have that working and we'll go from there.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: strange problem

    first thank you for fast response..
    second the algorithm is:
    for n check if all the numbers except 1 and it self divide without living a "rest" (% function) if its passes the test then the boolean variable stay true and the nomber will be shown on the screen...
    FINALLY i have succeeded the problem was that i forgot to "initialize" the boolean valeur to true after the inner loop....
    thanks a lot guys-really

     
    public class Premiers{
      public static void main(String[]args){
        int n=2;
        boolean p=true;
        for(int i=1;i<=100;i=i+1){
          for(int d=2;d<n;d=d+1){
            if(n%d==0){
              p=false;
            }
          }
          if(p!=false){
            Terminal.ecrireStringln(n+" est un n.primer");
          }
          n=n+1;
          p=true;
        }
      }
    }

  5. #5
    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: strange problem

    If your original problem statement is correct, then you need to continue the program until you find 100 prime numbers, not just those between zero and 100.

    You don't need to compare a boolean to false or true, you can simply check the boolean, as in:

    if ( p ) or if ( !p ).

    You can optimize your algorithm further. For example, there's no need to check even numbers after 2. There are other facts about finding prime numbers you can find with a quick search of the Internet that you could use.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: strange problem

    thank you for your help...
    i had already noticed the problem and i fixed it..
    i don't understood your [you can simply check the boolean, as in: if ( p ) or if ( !p )] if you can kindely explain...(sorry ,i am a beginner...)
    here is my new code...this time its do the job !
    public class Premiers{
      public static void main(String[]args){
        int n=2,pn=0;
        boolean p=true;
        while(pn<100){
          for(int d=2;d<n;d=d+1){
            if(n%d==0){
              p=false;
            }
          }
          if(p!=false){
            Terminal.ecrireStringln(n+" est un n.primer");
            pn=pn+1;
          }
          n=n+1;
          p=true;
        }
      Terminal.ecrireIntln(n);
      }
    }

Similar Threads

  1. Please help this is strange.
    By MillerJLee79 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2012, 04:01 AM
  2. Beginner programming- strange problem
    By jkkruse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 23rd, 2011, 05:39 AM
  3. Strange problem printing Unicode characters
    By sophist42 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 29th, 2011, 10:53 AM
  4. Strange launch problem using command prompt
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2010, 07:52 AM
  5. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 03:38 PM