8 Replies - 689 Views - Last Post: 13 December 2012 - 02:38 AM Rate Topic: -----

#1 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 336
  • Joined: 28-September 12

problem on enum

Posted 12 December 2012 - 07:27 PM

I have three file Book.java,BookRoyalty.java,Enuming.java. Book.java contain enum declared and Enuming.java contain main method when i compile this i get error:
D:\java_file>javac Enuming.java
Enuming.java:5: cannot find symbol
symbol  : class Book
location: class enums.Enuming
                Book[] books=Book.values();
                ^
Enuming.java:5: cannot find symbol
symbol  : variable Book
location: class enums.Enuming
                Book[] books=Book.values();
                             ^
Enuming.java:6: cannot find symbol
symbol  : class Book
location: class enums.Enuming
                for(Book book:books){
                    ^
Enuming.java:7: cannot find symbol
symbol  : variable BookRoyalty
location: class enums.Enuming
                        System.out.println(book.ordinal()+".The royalty of the b
ook" + book.toString()+"is"+BookRoyalty.calculateRoyalty(book));

                            ^
4 errors



Here are the code of three files
Book.java
package enums;

public enum Book{
	ORACLE,JAVAEE,JSP
}



BookRoyalty.java
package enums;


public class BookRoyalty{
	public static double calculateRoyalty(Book book){
		double royalty=0.00;

		if(book.equals(Book.ORACLE)){
			royalty=50000;
		}
	
		else if(book.equals(Book.JAVAEE)){
			royalty=100000;
		}
	
		else if(book.equals(Book.JSP)){
			royalty=25000;
		}
		return royalty;
	}
}



Enuming.java
package enums;

public class Enuming{
	public static void main(String[] args){
		Book[] books=Book.values();
		for(Book book:books){
			System.out.println(book.ordinal()+".The royalty of the book" +     
                        book.toString()+"is"+BookRoyalty.calculateRoyalty(book));
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem on enum

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: problem on enum

Posted 12 December 2012 - 09:42 PM

I do not get these errors, the code compiles fine. Are all your classes in the folder 'enums' when you are compiling?
Was This Post Helpful? 0
  • +
  • -

#3 amr.mostafa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 12

Re: problem on enum

Posted 12 December 2012 - 09:43 PM

i did run your code on netbeans and it's working correctly
Was This Post Helpful? 0
  • +
  • -

#4 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 336
  • Joined: 28-September 12

Re: problem on enum

Posted 13 December 2012 - 12:51 AM

Quote

I do not get these errors, the code compiles fine. Are all your classes in the folder 'enums' when you are compiling?

Yes i tried creating enums folder and placed all the 3 classes in it.When i compile there was no error but when executed look what i got
D:\java_file>javac enums\EnumTest.java

D:\java_file>java enums\EnumTest
Exception in thread "main" java.lang.NoClassDefFoundError: enums\EnumTest (wrong
 name: enums/EnumTest)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)



Was This Post Helpful? 0
  • +
  • -

#5 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 336
  • Joined: 28-September 12

Re: problem on enum

Posted 13 December 2012 - 01:13 AM

Quote

i did run your code on netbeans and it's working correctly


Yeah you're right,i got the answer on netbeans, but i want to try on command prompt and confused about getting error as shown above.
Was This Post Helpful? 0
  • +
  • -

#6 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: problem on enum

Posted 13 December 2012 - 01:49 AM

I believe the error is in using the backslash '\' instead of using forward slash '/' or '.' in your java statement. i.e. java enums.Enuming, or java enums/Enuming should work, atleast they do for me.

Edit: didn't see cfoleys post before edit, he wins.

This post has been edited by Mylo: 13 December 2012 - 02:07 AM

Was This Post Helpful? 2
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1935
  • View blog
  • Posts: 4,018
  • Joined: 11-December 07

Re: problem on enum

Posted 13 December 2012 - 01:55 AM

Use a backslash when compiling because you are referring to a file in a folder.

D:\java_file>javac enums\EnumTest.java


But when running the program you are specifying a java package and class, not a file and folder. You drop the file extension and change the slash for a dot.

java enums.EnumTest

Was This Post Helpful? 4
  • +
  • -

#8 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 336
  • Joined: 28-September 12

Re: problem on enum

Posted 13 December 2012 - 02:13 AM

View PostMylo, on 13 December 2012 - 01:49 AM, said:

I believe the error is in using the backslash '\' instead of using forward slash '/' or '.' in your java statement. i.e. java enums.Enuming, or java enums/Enuming should work, atleast they do for me.

Edit: didn't see cfoleys post before edit, he wins.

Hey thank you very much, its working

View Postcfoley, on 13 December 2012 - 01:55 AM, said:

Use a backslash when compiling because you are referring to a file in a folder.

D:\java_file>javac enums\EnumTest.java


But when running the program you are specifying a java package and class, not a file and folder. You drop the file extension and change the slash for a dot.

java enums.EnumTest

Even you are right. Thank you very much friends.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1935
  • View blog
  • Posts: 4,018
  • Joined: 11-December 07

Re: problem on enum

Posted 13 December 2012 - 02:38 AM

Glad you got it working. Mylo is a sneaky edit-ninja. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1