error: class, interface, or enum expected

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1402 Views - Last Post: 06 August 2013 - 07:19 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

error: class, interface, or enum expected

Posted 03 August 2013 - 08:25 PM

Just when I thought I was done, I get this last error: class, interface, or enum expected for line 180. My program was working just fine, until I added a couple for IF ELSE's and this came up. I checked online to see what this error means and its supposedly a syntax error.

I am really sorry that I keep asking you guys help for this program. I will no longer make up non-sense programs.



// this program will be consisted of multiple classes for starbucks
// also uses IGNORE CASE and SEARCHES keywords

// CONSTUCTORS ARE USED TO CREATE VARIABLES ON THE SPOT --> newboston 17

import java.util.Scanner;

public class Starbucks{

	public static void main(String[] args){
	
		Scanner input = new Scanner(System.in);
		String caffeVanilla2 = "milk, coffee, ice, vanilla bean powder, and whipped cream";
		String caramel2 = "milk, coffee, ice, caramel syrup, whipped cream, and caramel sauce";
		String crc2 = "caramel, milk, ice, dark caramel sauce, whipped cream, caramel drizzle, and crunchy sugar";

		//create object
		Frappuccinos frap = new Frappuccinos();
		
		//ask user what they want to study
		System.out.println("Which frappucino would you like to go over: caffe vanilla, caramel, or caramel ribbon crunch? ");
	
		String answer = input.nextLine();
		String cv = "caffe vanilla";
		String c = "caramel";
		String crc = "caramel ribbon crunch";
		
		String a1;
		String a2;
		String a3;
		String a4;
		String a5;
		String a6;
		String a7;
	
		//ask them to enter the ingredients of the drink they want to go over
		
		//match the ingredients for caffe vanilla
		if (answer.equalsIgnoreCase(cv)){
		
			System.out.println("Enter each (5) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
						
			if (caffeVanilla2.toLowerCase().contains(a1.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a2.toLowerCase()) &&  
				caffeVanilla2.toLowerCase().contains(a3.toLowerCase()) && caffeVanilla2.toLowerCase().contains(a4.toLowerCase()) && 
				caffeVanilla2.toLowerCase().contains(a5.toLowerCase())){
			
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCaffeVanilla();
			}
			
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");

			}
		}
		//match for caramel
		else if (answer.equalsIgnoreCase(c)){
		
			System.out.println("Enter each (6) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
			a6 = input.nextLine();

			if (caramel2.toLowerCase().contains(a1.toLowerCase()) && caramel2.toLowerCase().contains(a2.toLowerCase()) && 
				caramel2.toLowerCase().contains(a3.toLowerCase()) && caramel2.toLowerCase().contains(a4.toLowerCase()) && 
				caramel2.toLowerCase().contains(a5.toLowerCase()) && caramel2.toLowerCase().contains(a6.toLowerCase())){
				
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCaramel();
			}
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");
				frap.yesCaramel();
			}
		}
		
		//match for ribbon crunch
		else if (answer.equalsIgnoreCase(crc)){
			
			System.out.println("Enter each (7) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
			a6 = input.nextLine();
			a7 = input.nextLine();

			if (crc2.toLowerCase().contains(a1.toLowerCase()) && crc2.toLowerCase().contains(a2.toLowerCase()) &&
				crc2.toLowerCase().contains(a3.toLowerCase()) &&crc2.toLowerCase().contains(a4.toLowerCase()) &&
				crc2.toLowerCase().contains(a5.toLowerCase()) &&crc2.toLowerCase().contains(a6.toLowerCase()) &&
				crc2.toLowerCase().contains(a7.toLowerCase())){
				
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCRC();
				}
				
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");
				frap.yesCRC();
			}
		}
		
		else{
			System.out.println("Invalid option");
		}
		
		//ask if they would like a custom drink
		System.out.println("Would you like to customize your drink or add a custom drink? Enter yes or no: ");
		String yesOrNo = input.nextLine();
		String y = "yes";
		String n = "no";
		String answer2;
		
		// options for custom drinks
		if (yesOrNo.equalsIgnoreCase(y)){
			System.out.println("Ok which of the these frap drinks would you like to customize? " +
			"\ncaffe vanilla\ncaramel\ncaramel ribbon crunch: ");
			answer2 = input.nextLine();
			
			// match user input with drink
			if (answer2.equalsIgnoreCase(cv)){
				frap.customCV();
			}
			
			else if (answer2.equalsIgnoreCase(c)){
				frap.customC();
			
			}
		
			else if (answer2.equalsIgnoreCase(crc)){
				frap.customCRC();
			}
			
			else {
				System.out.println("Invalid entry");
			}		
		}
		
		else {
			System.out.println("Ok, no custom drinks");
		}
					
			System.out.println("The price of your drink will cost you one of your legs. If you are content \nwith your " + answer2 +
			" please enter yes to pay, otherwise enter no to cancel your entire order");
			
			String last = input.nextLine();
			
			//confirm order if yes
			if (last.equals(y)){
				System.out.println("I hope you enjoy your overpriced drink. Good day.");
			}
			//cancel if no
			else if (last.equals(n)){
			
				if (answer2.equalsIgnoreCase(cv)){
					frap.noCaffeVanilla(); // display cancel caffe vanilla
				}
				
				else if (answer2.equalsIgnoreCase(c)){
					frap.noCaramel(); // display cancel caramel
				}
				
				else if (answer2.equalsIgnoreCase(crc)){
					frap.noCRC(); // display cancel caramel ribbon
				}
				
			}
			
		}
	}
}

class Frappuccinos{

	Scanner input = new Scanner(System.in);
	
	//ingredients
	String caffeVanilla2 = "milk, coffee, ice, vanilla bean powder, whipped cream";
	String caramel2 = "milk, coffee, ice, caramel syrup, whipped cream, and caramel sauce";
	String crc2 = "caramel, milk, ice, dark caramel sauce, whipped cream, caramel drizzle, and crunchy sugar";
	
	//list of fraps
	boolean caffeVanilla = false;
	boolean caramel = false;
	boolean caramelRibbonCrunch = false;
	
	//constructor
	Frappuccinos(){
	}
	
	//make them available with ingredients or not
	public void yesCaffeVanilla(){
		caffeVanilla = true;
		System.out.println("The caffe vanilla contains: " + caffeVanilla2);
	}
	//DO SOMETHING WITH THIS
	public void customCV(){
		caffeVanilla = true;
		System.out.println("So you want a custom caffe vanilla...you hipster! \nWhat would you like in it? Enter each item followed by pressing ENTER," +
		" type in exit when you're done: ");
		
		String exit = "exit";
		String[] array = new String[7];
		
		//end the program if they enter exit
		for (int i = 0; i < array.length; i++){
			array[i] = input.nextLine();	
			//display what they entered
			System.out.println("This drink contains: " + array[i]); 
			if (array[i].equals(exit)){
				break;
			}
		}
			System.out.println("You are finshed with your order");
	}
			
	//use this for a cancellation order, ask if they would like to chnage their mind
	public void noCaffeVanilla(){
		caffeVanilla = false;
		System.out.println("Cancelling your caffe vanilla frap");
	}
	
	public void yesCaramel(){
		caramel = true;
		System.out.println("The ingredients for a caramel frap are: " + caramel2);
	}
	
	public void customC(){
		caramel = true;
		System.out.println("What would you like in your custom caramel frap? Enter each item followed by pressing ENTER, type in exit when you're done: ");
		
		String exit = "exit";
		String[] array = new String[7];
		
		//end the program if they enter exit
		for (int i = 0; i < array.length; i++){
			array[i] = input.nextLine();	
			//display what they entered
			System.out.println("This drink contains: " + array[i]); 
			if (array[i].equals(exit)){
				break;
			}
		}
			System.out.println("You are finshed with your order");		
	}
	
	public void noCaramel(){
		caramel = false;
		System.out.println("Cancelling your caramel frap...");
	}
	
	public void yesCRC(){
		caramelRibbonCrunch = true;
		System.out.println("The ingredients for a caramel ribbon crunch are: " + crc2);
	}
	
	public void customCRC(){
		caramelRibbonCrunch = true;
		System.out.println("What would you like in your caramel ribbon crunch? Enter each item followed by pressing ENTER, type in exit when you're done: ");
		
		String exit = "exit";
		String[] array = new String[7];
		
		//end the program if they enter exit
		for (int i = 0; i < array.length; i++){
			array[i] = input.nextLine();	
			//display what they entered
			System.out.println("This drink contains: " + array[i]); 
			if (array[i].equals(exit)){
				break;
			}
		}
			System.out.println("You are finshed with your order");
	}
	
	public void noCRC(){
		caramelRibbonCrunch = false;
		System.out.println("Cancelling your CRC...");
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: error: class, interface, or enum expected

#2 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:38 PM

I think you might be missing an if on line 151 or you have an extra bracket on line 178.
Was This Post Helpful? 2
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,655
  • Joined: 29-July 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:42 PM

The StarBucks class has one too many closing brackets, so delete the last "}" for that class. Now, where you delcare

String answer2;



you need to initialize it.

String answer2 = null;



It's a rule. It won't compile otherwise. I think its because local variables are not given default values like instance variables. I could be wrong, however.

This post has been edited by farrell2k: 03 August 2013 - 08:43 PM

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,740
  • Joined: 19-March 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:51 PM

"class, interface, or enum expected" is issued by the compiler when it's expecting a class, interface, or enum declaration. That is, it's at the outermost level of the code, and you're not. This is generally, as Matt suggests, because of a bracket mismatch or a missing control structure. I find it's easiest to keep your brackets matched when you use good and consistent code indentation (which you seem to do here, mostly) and also use "allman-style" bracement.

"Allman-style" aligns the braces which go together, like so:
public class Foo
{

  public static void main(String args[])
  {
    for (int i = 0; i <10; i ++)
    {
      System.out.println("Hello!");
    }
  }
}



You see how I can't possibly get confused about my indentation level? It's a little easier when the braces line up this way.

The other crucial thing is to keep your methods short, short, short. When you have a method (like your main here) that goes on for over 150 lines, it's very easy to get yourself screwed up. If you have lots of small methods, it's a lot easier to avoid this sort of problem.

The other nice thing about keeping methods short is that it reduces complexity. The less a function does, the easier it is to make sure it's doing it right. Breaking a problem down into small steps and executing each one correctly makes your programs work bettter.

Also, when you find yourself doing this:
		String a1;
		String a2;
		String a3;
		String a4;
		String a5;
		String a6;
		String a7;



something in your soul is probably whispering "use an array". Heed that voice.
Was This Post Helpful? 3
  • +
  • -

#5 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:51 PM

View Postfarrell2k, on 03 August 2013 - 08:42 PM, said:

The StarBucks class has one too many closing brackets, so delete the last "}" for that class. Now, where you delcare

String answer2;



you need to initialize it.

String answer2 = null;



It's a rule. It won't compile otherwise. I think its because local variables are not given default values like instance variables. I could be wrong, however.


So I should never let a user input just be something like

String answer2;

//but instead like 

String answer 2 = input.nextLine();


?
Was This Post Helpful? 0
  • +
  • -

#6 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:56 PM

View PostMATTtheSEAHAWK, on 03 August 2013 - 08:38 PM, said:

I think you might be missing an if on line 151 or you have an extra bracket on line 178.

Thanks it was an extra bracket
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,740
  • Joined: 19-March 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:56 PM

View Postfarrell2k, on 03 August 2013 - 10:42 PM, said:

where you delcare

String answer2;



you need to initialize it.

String answer2 = null;



It's a rule. It won't compile otherwise. I think its because local variables are not given default values like instance variables. I could be wrong, however.



I regret to say that this is incorrect. At least, the following compiles and runs without error.

public class Foo
{
   public static void main(String[] args)
   {
      String answer2; 

      System.out.println("It seems to have worked");
   }
}



It is indeed an error to refer to a value that has not yet been assigned to, which is probably what you meant, but it is not an error to simply have a declaration for an unused variable.
Was This Post Helpful? 1
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,655
  • Joined: 29-July 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 08:57 PM

Just initialize all local variables before referencing them. If you reference it, it MUST be initialized.

String answer2 = null;
answer2 = input.nextLine();


This post has been edited by farrell2k: 03 August 2013 - 08:58 PM

Was This Post Helpful? 2
  • +
  • -

#9 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:03 PM

View Postjon.kiparsky, on 03 August 2013 - 08:51 PM, said:

"class, interface, or enum expected" is issued by the compiler when it's expecting a class, interface, or enum declaration. That is, it's at the outermost level of the code, and you're not. This is generally, as Matt suggests, because of a bracket mismatch or a missing control structure. I find it's easiest to keep your brackets matched when you use good and consistent code indentation (which you seem to do here, mostly) and also use "allman-style" bracement.

"Allman-style" aligns the braces which go together, like so:
public class Foo
{

  public static void main(String args[])
  {
    for (int i = 0; i <10; i ++)
    {
      System.out.println("Hello!");
    }
  }
}



You see how I can't possibly get confused about my indentation level? It's a little easier when the braces line up this way.

The other crucial thing is to keep your methods short, short, short. When you have a method (like your main here) that goes on for over 150 lines, it's very easy to get yourself screwed up. If you have lots of small methods, it's a lot easier to avoid this sort of problem.

The other nice thing about keeping methods short is that it reduces complexity. The less a function does, the easier it is to make sure it's doing it right. Breaking a problem down into small steps and executing each one correctly makes your programs work bettter.

Also, when you find yourself doing this:
		String a1;
		String a2;
		String a3;
		String a4;
		String a5;
		String a6;
		String a7;



something in your soul is probably whispering "use an array". Heed that voice.


Lol thank you. I really do need to be consist and use those arrays more often. And as for using methods, you couldnt be more correct. I've been noticing that (when look at others codes for fun) I am the only one who has a super long main method. For my next code I will try to keep my main as short as possible.
Was This Post Helpful? 0
  • +
  • -

#10 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:06 PM

Yeah, you have an extra brace hanging near the end of your Starbucks class. After removing a brace at the end of the first class, it compiled fine with no errors.

This post has been edited by salazar: 03 August 2013 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

#11 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:06 PM

View Postfarrell2k, on 03 August 2013 - 08:57 PM, said:

Just initialize all local variables before referencing them. If you reference it, it MUST be initialized.

String answer2 = null;
answer2 = input.nextLine();



This entire time I've been programming, I never really understood those keywords like initilaze. But now I think I get it:
if its pretty much not a constant (like a user input) then it must be written out all at once like
String answer = input.nextLine();


Am I right?
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,740
  • Joined: 19-March 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:10 PM

Here's a thing you can try. Look at the way you have the same code over and over again, stuff like


else if (answer.equalsIgnoreCase(crc)){
			
			System.out.println("Enter each (7) ingredient, followed by ENTER for " + answer + ":");
			a1 = input.nextLine();
			a2 = input.nextLine();
			a3 = input.nextLine();
			a4 = input.nextLine();
			a5 = input.nextLine();
			a6 = input.nextLine();
			a7 = input.nextLine();

			if (crc2.toLowerCase().contains(a1.toLowerCase()) && crc2.toLowerCase().contains(a2.toLowerCase()) &&
				crc2.toLowerCase().contains(a3.toLowerCase()) &&crc2.toLowerCase().contains(a4.toLowerCase()) &&
				crc2.toLowerCase().contains(a5.toLowerCase()) &&crc2.toLowerCase().contains(a6.toLowerCase()) &&
				crc2.toLowerCase().contains(a7.toLowerCase())){
				
				System.out.println("Congrats you got ALL of the ingredients right!");
				frap.yesCRC();
				}
				
			else{
				System.out.println("Im sorry. At least one of your inputs are incorrect");
				frap.yesCRC();
			}


This could be refactored, and you could lose a lot of needless repetition.


You might want to have a method which would get N inputs from the user, and return them as an array. It should construct a prompt like the one you're using.
You would then have a method which would compare an array of Strings to another array of Strings and decide if they have exactly the same contents, ignoring case. (you might want to also ignore leading and trailing whitespace). You could use those two methods to shorten your code considerably.
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,740
  • Joined: 19-March 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:25 PM

View Poststreek405, on 03 August 2013 - 11:06 PM, said:

View Postfarrell2k, on 03 August 2013 - 08:57 PM, said:

Just initialize all local variables before referencing them. If you reference it, it MUST be initialized.

String answer2 = null;
answer2 = input.nextLine();



This entire time I've been programming, I never really understood those keywords like initilaze. But now I think I get it:
if its pretty much not a constant (like a user input) then it must be written out all at once like
String answer = input.nextLine();


Am I right?



"Initialize" simply means that you give it a value to start with. In many languages, a variable will get a "default" value depending on its type, or else it will have a garbage value (whatever was left over in that piece of memory). It's always been considered good practice to overwrite the default immediately unless you actually know that it's the value you want - even then, many people will explicitly set it to that value, just to make the code more readable.
Java requires this initialization in many but not all cases. For example, when you initialize an array, you get its elements initialized for free. However, a naked String or other variable is initially unset, and it has to appear on the left hand side of an assignment before it can appear on the right hand side. (This initialization requirement is part of Java's reaction to the generally unsafe programming styles common in the '80s and '90s, which were mostly a result of smaller machines and tighter tolerances. Basically, by the time they were building Java it was obvious that we could afford to write safer code, and so they decided to require it)

In both of these examples, answer is set to the user input, so you're fine there.
Was This Post Helpful? 1
  • +
  • -

#14 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,655
  • Joined: 29-July 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:28 PM

View Poststreek405, on 04 August 2013 - 04:06 AM, said:

View Postfarrell2k, on 03 August 2013 - 08:57 PM, said:

Just initialize all local variables before referencing them. If you reference it, it MUST be initialized.

String answer2 = null;
answer2 = input.nextLine();



This entire time I've been programming, I never really understood those keywords like initilaze. But now I think I get it:
if its pretty much not a constant (like a user input) then it must be written out all at once like
String answer = input.nextLine();


Am I right?


No. To initialize means to give something a value. When a variable is declared with local scope (in a method or constructor), it is not given a default value (initialized). You cannot reference something until it has been initialized. When you do this in local scope:

String answer;



you tell the compiler that you want to allocate memory for a String variable named answer. That is all that happens. A place in memory is created to hold the String answer, an empty block with nothing to access.

Now, when you:

String answer = null;



You actually assign something to is, a null value, so now there is something accessible. Now you can freely reference(access) answer with:

answer = input.nextLine();



That's the best way I can explain it.

Maybe someone more knowledgeable like jon.kiparsky can asplain it better.

This post has been edited by farrell2k: 03 August 2013 - 09:32 PM

Was This Post Helpful? 2
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,740
  • Joined: 19-March 11

Re: error: class, interface, or enum expected

Posted 03 August 2013 - 09:35 PM

View Postfarrell2k, on 03 August 2013 - 11:28 PM, said:

Maybe someone more knowledgeable like jon.kiparsky can asplain it better.


Nope. I can't do much better than that.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2