-3

I've written the following Java in attempt to find primes less than 1000:

public class primes {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("2"); 
        int n=2;
        While (n<1000);
        {
            for(int d = 2; d<n; d++); //if d|n abort divisors loop and try next number
            {

                if (n%d == 0){ //if d|n try next number
                    n++;
                    break;
                }

                if (d>(n/2)){ //if there are no divisors up to n/2 n is prime, print n then try next number
                    System.out.println(n);
                    n++;
                    break;
                }
                d++; //try next divisor

            }           
        }
    }
    private static void While(boolean b) {
        // TODO Auto-generated method stub  
    }
}

I get bugs each time d is called in the inner loop that it is not declared as a variable. But I declared in in the for statement. I've read several examples where you can do this. What's wrong here, and how to resolve?

juzraai
  • 5,693
  • 8
  • 33
  • 47
vap
  • 151
  • 1
  • 2
  • 9
  • You inner loop never gets executed, since d < n is always false, so how can you get an error inside it? – Joakim Danielson Jun 24 '18 at 20:37
  • the loop is empty `for(...) /*nothing here */ ;` - remove the semicolon at the end. Same true for the while `while(..) ;` if it was a valid while – user85421 Jun 24 '18 at 20:39
  • boys you skiped endless loop on beginning while (n<1000); :) :) – Peter1982 Jun 24 '18 at 20:40
  • 2
    You have serious syntax problems. I advise you to **start learning Java from the very beginning.** You put semicolons after while/for loop heads, and also you declared an empty `While` method, instead of using the `while` loop. – juzraai Jun 24 '18 at 20:41
  • @Peter1982 you missed the capital `W` of `While` in the question [:-) – user85421 Jun 24 '18 at 20:44
  • I would guess that you wrote the "`While(...)`"-loop first, got an error from your IDE and then hit auto-correct a buch of times. As @juzraai mentioned, you should start learning Java from scratch. The [official Oracle tutorials](https://docs.oracle.com/javase/tutorial/) are pretty throughout. – Turing85 Jun 24 '18 at 20:44
  • @CarlosHeuberger nope I have a lowercase in answer :-) – Peter1982 Jun 24 '18 at 20:45

1 Answers1

0

Here is your corrected code (contained in main(String[] args), not in a class):

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("2"); 
    int n=2;
    while (n<1000)
    {
        for(int d = 2; d<n; d++) //if d|n abort divisors loop and try next number
        {

            if (n%d == 0){ //if d|n try next number
                break;
            }

            if (d>(n/2)){ 
                //if there are no divisors up to n/2 n is prime, print n
                //then try next number
                System.out.println(n);
                n++;
                break;
            }

        }  
        n++;
    }
}

However, you need to learn (or relearn) the language. You stumped me for a few minutes with your SEMICOLONS after the while and for loops. I also discovered many more errors with the code. I believe that you can code well, but you need to learn the basics of Java first.

jcroskery
  • 135
  • 2
  • 10
  • 1
    Thank you for your feedback, that really helps. You're correct, I am rusty. I haven't written a Java program in about four years. – vap Jun 24 '18 at 21:31