Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Can't compile simple program to calculate primenumber

    Can't compile simple program to calculate prime number

    Java Code:
    Class primo {
      public static void main(String[] args) { 
        int cont=2;
        for (int i=3; i<1001; i++) {
          boolean primo = true;
          for (int j=2; j<i; j++) {
            if ((i%j) == 0) {
    	  boolean = false;
    	  break;
    	}
          }
          if (boolean == true) 
            System.out.println ("E primo: "+i);
    	cont++;
        }	 	
        System.out.println ("numero de primos: "+cont);
      }   
    }
    The errors that I receive when I try to compile it:

    primo.java:1: class, interface, or enum expected
    Class primo {
    ^
    primo.java:2: class, interface, or enum expected
    public static void main(String[] args) {
    ^
    primo.java:4: class, interface, or enum expected
    for (int i=3; i<1001; i++) {
    ^
    primo.java:4: class, interface, or enum expected
    for (int i=3; i<1001; i++) {
    ^
    primo.java:4: class, interface, or enum expected
    for (int i=3; i<1001; i++) {
    ^
    primo.java:6: class, interface, or enum expected
    for (int j=2; j<i; j++) {
    ^
    primo.java:6: class, interface, or enum expected
    for (int j=2; j<i; j++) {
    ^
    primo.java:6: class, interface, or enum expected
    for (int j=2; j<i; j++) {
    ^
    primo.java:9: class, interface, or enum expected
    break;
    ^
    primo.java:10: class, interface, or enum expected
    }
    ^
    primo.java:14: class, interface, or enum expected
    cont++;
    ^
    primo.java:15: class, interface, or enum expected
    }
    ^
    primo.java:17: class, interface, or enum expected
    }
    ^
    13 errors
    I checked the number of opened and closed {} and it's the same, five, so I don't know what is going on.
    Thanks for the support

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Can't compile simple program to calculate primenumber

    You need to add public before Class and change Class to class. Have you read the basics about Java?

  3. #3
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: Can't compile simple program to calculate primenumber

    Quote Originally Posted by Wnt2bsleepin View Post
    You need to add public before Class and change Class to class. Have you read the basics about Java?
    Yes.

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Can't compile simple program to calculate primenumber

    ok, just checking. We all make mistakes. If you make those changes, it still won't compile btw. If you are using an IDE it should help you out.

  5. #5
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: Can't compile simple program to calculate primenumber

    Quote Originally Posted by Wnt2bsleepin View Post
    ok, just checking. We all make mistakes. If you make those changes, it still won't compile btw. If you are using an IDE it should help you out.
    Compiled as soon as I changed class from uppercase to lowercase. I didn't realized that Class was with the same colors as System.out.println. Should have noticed that. Anyway, was already a couple of years since I last saw Java. Thanks for the help.

Similar Threads

  1. Program in Java To calculate GCD of n numbers.?
    By ankitsinghal_89 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 09:23 AM
  2. Replies: 12
    Last Post: 02-08-2011, 02:58 PM
  3. Replies: 1
    Last Post: 02-08-2011, 09:11 AM
  4. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  5. Why my program cannot calculate the decimal value?
    By pearllymary78 in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 12:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •