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

Thread: What's wrong with my code? Are there any errors? Is the idea right? Please helpThanks

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code? Are there any errors? Is the idea right? Please helpThanks

    //I want to know if this program is right. The program uses loops to find the first 400 primes.
    class Primes {
    public static void main(String[] arguments) {
    int counTer2 = 0;
    primeLoop:
    while (counTer2 < 401) {
    for (int prime = 2; prime<prime+1; prime++) {
    for (int number = 1;number <prime+1; number++) {
    int counter = 0
    if (prime%number == 0)
    counter = counter + 1;
    }
    if (counter == 2)
    System.out.println("#:" + prime);
    counTer2 = counTer2 + 1;
    }
    if (counter2 == 401)
    break primeLoop;
    }
    }
    }


  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: What's wrong with my code? Are there any errors? Is the idea right? Please helpTh

    Quote Originally Posted by Chris511026 View Post
    //I want to know if this program is right.
    Does the code compile? Is the output that which you would expect?

    As a side note, consider using code tags when posting your code as this makes it much easier to read. For instance:

    class Primes {
    public static void main(String[] arguments) {
        int counTer2 = 0;  
        primeLoop:
        while (counTer2 < 401) {
          for (int prime = 2; prime<prime+1; prime++) {
             for (int number = 1;number <prime+1; number++) {
                int counter = 0    
                if (prime%number == 0)
                counter = counter + 1;
                }
             if (counter == 2)
                 System.out.println("#:" + prime);
                 counTer2 = counTer2 + 1;
          }
          if (counter2 == 401)
          break primeLoop; 
        }
      }
    }

    To use these, you would wrap your code with these tags: [code=java] [/code]

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What's wrong with my code? Are there any errors? Is the idea right? Please helpTh

    Suggested reading

Similar Threads

  1. weird errors in my code
    By jobje325 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 11:15 AM
  2. Getting Exception Errors, But Not Sure How To Fix Code
    By noel222 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 12:09 PM
  3. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  4. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM
  5. Please check the code for the errors
    By nrao in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 16th, 2010, 05:37 PM