9 Replies - 1272 Views - Last Post: 01 November 2011 - 07:36 PM Rate Topic: -----

#1 whatheh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-September 11

class, interface, or enum expected errors

Posted 01 November 2011 - 06:53 PM

I am working on an assignment where I am supposed to make my own boolean methods to determine if a number is a prime, and then a super prime.
( a super prime is where d1d2d3, d1d2 and d1 are all primes ex: 797 is a super prime because 797, 79 , and 7 are all prime)

The program overall is to take a user inputted amount of times the program is supposed to run,
and then each run through take an integer input and output the closest superprime to that number. Part of the assignment is I HAVE to have the 2 boolean methods.
Sample run that was given for the assignment:

"Sample run:
Enter the number of experiments N = 3
1. Number = 4501
The nearest super prime to 4501 is 3797
2. Number = 785
The nearest super prime to 785 is 797
3. Number = 5200
The nearest super prime to 5200 is 5939 "


Anyway, after attempting this for hours, I could really use some help, I brought it down from ~25 errors to 5, and all 5 errors are in one line of code but I am not sure what to do from here. Any help at all is greatly appreciated. I hope my sloppy variable usage isn't too confusing...

import java.util.*; //import the scanner

public class Superprime
{
   public static void main(String[] args)
   {
   
      Scanner kb = new Scanner(System.in);  //declare scanner variable
      int x,number,p; // declare other variables for #experiments and the number to be tested
      
      System.out.println("Enter the number of experiments you want to run:");
      x = kb.nextInt();
      
      int y,z; //variables for while loops
      int q=p; //initialize a second variable =p to run in a loop
      int r=p; // initialize a 3rd variable =p to run in a second loop

      // this is to run the experiment as many times as the user wants to
      for (number = 0; number <=x; number++)
      {
      //each run of the experiment will get an input value to be tested
     	 System.out.println("Enter the number you want to test.");
    	 g= kb.nextInt();
      
      //boolean statements
      while (!(issuperprime(g)))
         y=g++;
      while (!(issuperprime(g)))
         z=g--;
      if ((g-z)>(y-g))
         {
         System.out.println("Number = " + g);
         System.out.println("The closest super prime to " + g
                            + " is " + y);
      }
      else if ((g-z)<(y-g))
      {
      System.out.println("Number = " + g);
      System.out.println("The closest super prime to " + g
                            + " is " + z);
      }
      else if ((g-z)==(y-g))
      {
      System.out.println("Number = " + g);
      System.out.println("The closest super prime to " + g + " is both "
                         + y + " and " + z);
                         
      }
      else 
      System.out.println("Error");
      }
    

   //prime method
   public static boolean isprime(int p)
   {
    if (p%2==0)
        return false;
    for(int i=3;i*i<=p;i+=2)
      {
      if(p%i==0)
         return false;
         break;
      }

     return true;

   }
}

//Super prime method
   public static boolean issuperprime(int sp)
   {
   sp=g;
   while (sp>=10)
   { 
   if (!(isprime(p)))
      {
      return false;
      break;
      }
   else if (isprime(p))
      sp=sp/10;
    }
      return true;
    }  

}

 


The errors are all in line 55, which is my first boolean method to determine if a number is prime.

Superprime.java:59: illegal start of expression
public static boolean isprime(int p)
^
Superprime.java:59: illegal start of expression
public static boolean isprime(int p)
^
Superprime.java:59: ';' expected
public static boolean isprime(int p)
^
Superprime.java:59: '.class' expected
public static boolean isprime(int p)
^
Superprime.java:59: ';' expected
public static boolean isprime(int p)
^

Er, the title of the post was made when I had 6 errors elsewhere that were all class, interface, or enum expected errors. I forgot the change it, please disregard the title since it has nothing to do with the post....sorry!

Is This A Good Question/Topic? 0
  • +

Replies To: class, interface, or enum expected errors

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 06:56 PM

Before that method declaration add a '}' to close your main method. ^^
Was This Post Helpful? 1
  • +
  • -

#3 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:01 PM

@fromTheSprawl I checked this time (f5) before a posted and you replied in between. You are super fast! I am just gonna go to bed, looks like this forum is handled lol.

@whatheh You should use better indentations so you don't get confused. Usually every time something is contained in a block of statements like so:

public static void main(String[] args)
{
   // Everything here is indented 1 tab
}

This post has been edited by Sheph: 01 November 2011 - 07:02 PM

Was This Post Helpful? 1
  • +
  • -

#4 whatheh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-September 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:04 PM

Thank you very much! It got rid of those errors but I am back to the class, interface, or enum expected errors I had originally.

Superprime.java:75: class, interface, or enum expected
while (sp>=10)
^
Superprime.java:80: class, interface, or enum expected
break;
^
Superprime.java:81: class, interface, or enum expected
}
^
Superprime.java:84: class, interface, or enum expected
}
^
Superprime.java:86: class, interface, or enum expected
}
^

These are all in or after my issuperprime method. I had read something earlier (I've been at this for...EVER) that said this type of error implies mistakes earlier in the code,
is this correct? (If so I'm assuming I made nonstop horrific errors), or is there a simple-ish fix I can do for this?

View PostSheph, on 01 November 2011 - 07:01 PM, said:

@fromTheSprawl I checked this time (f5) before a posted and you replied in between. You are super fast! I am just gonna go to bed, looks like this forum is handled lol.

@whatheh You should use better indentations so you don't get confused. Usually every time something is contained in a block of statements like so:

public static void main(String[] args)
{
   // Everything here is indented 1 tab
}


Sorry, all my assignments up till now have been very ocd neat about spacing, I'm just really short on time and incredibly confused for the first time all semester so it's pretty sloppy.



... :'(
Was This Post Helpful? 0
  • +
  • -

#5 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:14 PM

I got your code to run I had to intialize a bunch of variables and random errors, but most of the stuff your errors talk about were here:

//Super prime method
   public static boolean issuperprime(int sp)
   {
	   while (sp>=10)
	   { 
		   if (!(isprime(sp)))
		      return false;
		   else if (isprime(sp))
		      sp=sp/10;
	   }
	   return true;
   }

This post has been edited by Sheph: 01 November 2011 - 07:14 PM

Was This Post Helpful? 1
  • +
  • -

#6 whatheh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-September 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:21 PM

Thank you, it brought my compiling errors from 6 to 4 and looks alot better. Can I have a hint about these random errors? Are they the cause of my sloppy variables in the main method?
Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:25 PM

Well let me see if i can repost the updated code and comment where I changed.

public class SuperPrime
{
   public static void main(String[] args)
   {
   
      Scanner kb = new Scanner(System.in);
      int x,number,p;
      
      System.out.println("Enter the number of experiments you want to run:");
      x = kb.nextInt();
      
      int y = 0,z = 0; // 
      int q=0; // Initialized everything here to 0, got rid of p since it isn't even initialized at this point
      int r=0; //

      for (number = 0; number <=x; number++)
      {
	     System.out.println("Enter the number you want to test.");
	     int g= kb.nextInt(); // declared g as a int here
	      
	      while (!(issuperprime(g)))
	         y=g++;
	      while (!(issuperprime(g)))
	         z=g--;
	      if ((g-z)>(y-g))
	         {
	         System.out.println("Number = " + g);
	         System.out.println("The closest super prime to " + g
	                            + " is " + y);
	      }
	      else if ((g-z)<(y-g))
	      {
	      System.out.println("Number = " + g);
	      System.out.println("The closest super prime to " + g
	                            + " is " + z);
	      }
	      else if ((g-z)==(y-g))
	      {
	      System.out.println("Number = " + g);
	      System.out.println("The closest super prime to " + g + " is both "
	                         + y + " and " + z);
	                         
	      }
	      else 
	      System.out.println("Error");
      }
   }
    

	   //prime method
	   public static boolean isprime(int p) {
	    if (p%2==0)
	        return false;
	    for(int i=3;i*i<=p;i+=2)
	      {
	      if(p%i==0)
	         return false; // removed break ...pointless if you return first
	      }
	
	     return true;
	
	   }

//Super prime method - already posted fixes
   public static boolean issuperprime(int sp)
   {
	   while (sp>=10)
	   { 
		   if (!(isprime(sp)))
		      return false;
		   else if (isprime(sp))
		      sp=sp/10;
	   }
	   return true;
   } 

}


Note that your output is incorrect when it runs. Here is my result:

Enter the number of experiments you want to run:
1
Enter the number you want to test.
123
Number = 131
The closest super prime to 131 is 130

This post has been edited by Sheph: 01 November 2011 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:27 PM

Sheph, don't sleep. I'm a passive helper at best! Many students need your help! Okay thread hijack over. ^^
Was This Post Helpful? 0
  • +
  • -

#9 whatheh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-September 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:34 PM

Thank you SO much, it finally ran.
When I first made your changes it came up with 16 errors I was terrified lol, but I just deleted one too many } or something.

hmmm well if the outputs incorrect thats something I can handle ( I hope). Just getting it to compile is so relieving, thank you!
Was This Post Helpful? 0
  • +
  • -

#10 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: class, interface, or enum expected errors

Posted 01 November 2011 - 07:36 PM

Now you can see what messy code unorganized code does. It probably takes you longer to fix all the problems that you hide when you write code like this than if you took the time to hit tab a few times. But, glad you got it running for now.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1