aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Scjp6 enum question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Scjp6 enum question" Watch "Scjp6 enum question" New topic
Author

Scjp6 enum question

Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
Hi All,

I am using Kathy Sierra and Bert Bates book for scjp6 and getting compile error for below code snippet. Please help me?

enum CoffeeSize {
BIG(8), SMALL(6), GRANDE(12)
CoffeeSize size(int ounces) {
this.ounces = ounces;
}
private int ounces;
public int getOunces() {
retrun ounces;
}
}

public class TestServer {
TestServer size;
public static void main (String args[]) {
TestServer drink1 = new TestServer();
drink1.size = CoffeeSize.BIG;

TestServer drink2 = new TestServer();
drink2.size = CoffeeSize.GRANDE;

System.out.println("printing Big size " + drink1.size.getOunces());
for(CoffeeSize cs: CoffeeSize.valueOf())
System.out.println(cs + " " + cs.getOunces());

}
}

Error:
D:\Java>javac TestServer.java
TestServer.java:1: 'class' or 'interface' expected
enum CoffeeSize {
^
TestServer.java:22: ';' expected
for(CoffeeSize cs: CoffeeSize.valueOf())
^
TestServer.java:25: illegal start of expression
}
^
TestServer.java:24: ';' expected

^
4 errors

D:\Java>
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
Always use code tags when posting codes in the forum . The Corrected code is given below. Check out the changes made yourself . If unable to find do tell it , would help ..


Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
I compiled the below code, but complie errors again.

How to place the code in code tag?



ERROR:

D:\Java>javac TestServer.java
TestServer.java:1: 'class' or 'interface' expected
enum CoffeeSize {
^
TestServer.java:29: ';' expected
for(CoffeeSize cs: CoffeeSize.values())
^
TestServer.java:32: illegal start of expression
}
^
TestServer.java:30: ';' expected
System.out.println(cs + " " + cs.getOunces());
^
4 errors
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
Seems to be installation problem .. re-check it ..
Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
1. What installation problem?be more specific
2. Simple java classess are compiling fine
3. My java class path is D:\j2sdk1.4.2_07

Am I doing anything wrong?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

why are you using old version of java viz java 1.4 . that has been outdated long back. also for-each loop was introduced in java 1.5 so it won't work if you are compiling it in jdk 1.4

i recommend you download latest version of java from the oracle site and then compile your code.

also your other question also has got the same problem

also enums werent there in java until java 5
waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

I agree with Gurpreet, it seems that the issue is due to the old java version you are using.
Srinivas Palam
Ranch Hand

Joined: Oct 20, 2005
Posts: 51
Thanks All. Yes, it was old version issue. I have downloaded jdk1.5 and the programs compiled successfully.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Srinivas Palam wrote:Thanks All. Yes, it was old version issue. I have downloaded jdk1.5 and the programs compiled successfully.



why still java 1.5 ? latest version of java is java 7. also java 6 will be discontinued support from february next year. if you are preparing for ocpjp 6 then you should/must use java 6 or 7. there are some additions for e.g using enums in switch statement that were added in java 6. so i recommend to download latest version.
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scjp6 enum question