Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scjp6 enum question

 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to be installation problem .. re-check it ..
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Gurpreet, it seems that the issue is due to the old java version you are using.
 
Srinivas Palam
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All. Yes, it was old version issue. I have downloaded jdk1.5 and the programs compiled successfully.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic