Forums Register Login

Head First Java Frustration!

+Pie Number of slices to send: Send
I'm about to remove my reference variable to my Head First Java book, so that it will end up on the garbage collection heap and go away. (huh maybe I am learning something)

I have been struggling through this book, mostly because of complier errors. It seems that reserved words are extremely case sensitive; using IF instead of if gives a compiler error, system.out.print instead of System.out.print gives you a compiler error as well. It took me awhile to figure that out! (grrrr)

I'm only on page 64 and I'm seriously thinking about tossing this book.
Just now I tried to work out the Code Magnet exercise on page 64. I came up with the right answer (according to the answer page) but when I try to compile the code I get this error;

Exception in thread "main" java.lang.NoClassDefFoundError: TestArrays/java

I have no idea what that means.

Here�s my code for those pleasant souls who don�t have this book that might be willing to help me;



Output should be;
Island = Fiji
Island = Cozumel
Island = Bermuda
Island = Azores

I have to walk away from this now so I can calm down.

Thanks in advance
Tom
[ July 12, 2005: Message edited by: Mark Spritzler ]
+Pie Number of slices to send: Send
 

using IF instead of if gives a compiler error



Yes, Java is a case-sensitive language. That's something that you are going to have to come to terms with right off the bat or you'll be taking a whole lot of "cool down walks".

Why does that make you frustrated with the book? Is the case incorrect in the examples?
+Pie Number of slices to send: Send
Java is case sensitive, and that can be very irritating at first! As for your compilation error, I have a feeling you're making a typo when compiling the class. Instead of javac TestArray.java, it looks like you might have typed javac TestArray/java.

Good luck!
+Pie Number of slices to send: Send
On second thought, it could also be a classpath issue, in which case you've encountered the second most irritating feature of Java (configuring your classpath). I promise you that things will get better from here on out!
+Pie Number of slices to send: Send
Any language can be frustrating in the beginning. The problems you're describing don't seem to be related to the book. Most languages I've worked with are case-sensitive, especially with keywords. Only toy languages like BASIC are not, but that's probably not true in all implementations either.

I can only recommend patience, attention to detail (typos are the #1 case of programming errors), and a sense of humor. Oh, and don't leave a loaded hunting rifle too close to your computer, or you may have to buy a new pc!
+Pie Number of slices to send: Send
First thing why it isn't compiling.

The class needs to be public

so



Mark
+Pie Number of slices to send: Send
He typed

java TestArray.java

The Java runtime (java.exe) thinks you want to run a class named "java" in a package named "TestArray".

"Compiling" means running the compiler "javac". The main argument to the compiler is the name of a Java source code file; these always must be named *.java, and you always must give the complete filename to the compiler. You feed a *.java file to the compiler like

javac TestArray.java

This produces a file named TestArray.class .

"Running" the program means executing the runtime program "java". "java" doesn't use *.java files -- it has no idea what they are. It uses *.class files. But here's the tricky part: the arguments to "java" as not file names -- they're class names. Since you've written a class named TestArray, you must run it using

java TestArray

If you say "java TestArray.java", then Java goes looking for a class named "TestArray/java" (both the dot and the slash are used a separators in class names; the dot is used in source code and on the command line, while the slash is used internally -- which is why you see it in the error message.)

As for the case sensitivity: that's true for the majority of modern computer languages; Java's not alone here.
+Pie Number of slices to send: Send
 

Originally posted by Mark Spritzler:
First thing why it isn't compiling.

The class needs to be public

so



Mark




Actually it still ran in my IDE.

The other aspect of why it wouldn't compile is always classpath.

So what does classpath mean.

Simply put you set the base directory from which all your code will be in.

So if I put all my code in

c:\myJavaApp\src\

then that is what I set my classpath too.

If I have TestArrays.java in that directory, then I go to that directory in DOS and type "javac TestArrays.java". <B>Now if you type 'java -cp . TestArrays' from this directory you will get your code to run </B>

And as you might have learned already. There are packages to better group your classes. So if you put TestArrays into the com.yournamehere package, then you need to create a c:\myJavaApp\src\com\yournamehere directory and place the TestArrays.java file in that directory.

Now , here is the sometimes tricky part. You still keep your classpath set to "c:\myJavaApp\src". So now when you want to compile you have your DOS prompt in the c:\myJavaApp\src directory and type "javac com.yournamehere.TestArrays.java" and to run it just like above you use. "java -cp . com.yournamehere.TestArrays"

you can set your classpath using an environment variable in Windows. so in DOS you can type "set classpath=c:\myJavaApp\src" and now you no longer need that "-cp ." in the java call. So now to run it...

"java TestArrays" - no packages here

or

"java com.yournamehere.TestArrays" if the TestArrays.java file is in a package and in the c:\myJavaApp\src\com\yournamehere directory.

Mark
+Pie Number of slices to send: Send
Originally posted by Thomas Hasselbeck:
I have been struggling through this book, mostly because of complier errors. It seems that reserved words are extremely case sensitive; using IF instead of if gives a compiler error, system.out.print instead of System.out.print gives you a compiler error as well. It took me awhile to figure that out!


Not only reserved words are case sensitive; every word in Java is case sensitive.

when I try to compile the code I get this error;

Exception in thread "main" java.lang.NoClassDefFoundError: TestArrays/java


"javac TestArrays.java"
usually does not give that error.

Perhaps you typed
"java TestArrays/java"
??
+Pie Number of slices to send: Send
 

Originally posted by Mark Spritzler:
First thing why it isn't compiling.

The class needs to be public



A class does not need to be public to compile. It's pretty much only when you start getting into packages that you need to worry about whether or not it is public.
+Pie Number of slices to send: Send
Also, if you are sitting in the directory where the *.java file is when you compile, you don't need to worry about the classpath issue.

e.g. type
C:\Java>javac TestArrays.java

and you should see a
C:\Java>TestArrays.class file in the same directory.

(I like to start as simple as possible.)
+Pie Number of slices to send: Send
So Thomas, as you can see there's no dearth of people willing to help you out here! Hang in there guy, and we'll help you through all this.
+Pie Number of slices to send: Send
 

Originally posted by Marilyn de Queiroz:


A class does not need to be public to compile. It's pretty much only when you start getting into packages that you need to worry about whether or not it is public.



Actually in my second post, I rephrased that. I meant to run.

Mark
+Pie Number of slices to send: Send
Holy Smokes! I'm completely humbled by the responses I got.

Yeah I typed in �Java TestArrays.java�
When I typed in �Java TestArrays� I saw the desired output.

I think part of my problem is that I�m a product of VB programming and I�m use to some of the creature comforts that application provides. Plus I was using Word to make my java files (I�m now using notepad) and Word automatically capitalizes words on you. I guess the case sensitive point was not driven home when reading the book and during one of my previous walk-aways I forgot that to run the java program you leave off the .java part.

When I�m finished wiping the egg from my face I will continue on with my Java journey.
And it�s very comforting to know that there is a bartender close by� I�ll need a few stiff belts before the journey is over.

Thanks again.
+Pie Number of slices to send: Send
 

Originally posted by Thomas Hasselbeck:
Holy Smokes! I'm completely humbled by the responses I got.

Yeah I typed in �Java TestArrays.java�
When I typed in �Java TestArrays� I saw the desired output.

I think part of my problem is that I�m a product of VB programming and I�m use to some of the creature comforts that application provides. Plus I was using Word to make my java files (I�m now using notepad) and Word automatically capitalizes words on you. I guess the case sensitive point was not driven home when reading the book and during one of my previous walk-aways I forgot that to run the java program you leave off the .java part.

When I�m finished wiping the egg from my face I will continue on with my Java journey.
And it�s very comforting to know that there is a bartender close by� I�ll need a few stiff belts before the journey is over.

Thanks again.





Great, I am glad you got it too work. I also have experience with VB and understand your plight. Don't worry, it will eventually, some day, get easier.

Word is a pain when it automatically capitalized things on you.

You can try some other text editors like TextPad, or JEdit, that are Java friendly.

Mark
+Pie Number of slices to send: Send
A class does not need to be public to run.

Regarding text editors, there are many that are much better in my opinion than notepad. Mark named a few. Also, UltraEdit and even DOS's old 'edit' program (at least it maintains the indents). I think that Word and Notepad are two of the worst.
+Pie Number of slices to send: Send
 

Originally posted by Marilyn de Queiroz:
A class does not need to be public to run.

Regarding text editors, there are many that are much better in my opinion than notepad. Mark named a few. Also, UltraEdit and even DOS's old 'edit' program (at least it maintains the indents). I think that Word and Notepad are two of the worst.



When the class was not public, and I ran java, it gave the error, when I added public to the class, then it would run through java.

Mark
+Pie Number of slices to send: Send
 

Originally posted by Thomas Hasselbeck:
Holy Smokes! I'm completely humbled by the responses I got.



Tom, don't be humbled! A beginning book should make such things clear ... NOTHING should be left to the imagination.

Best,

Jacquie
+Pie Number of slices to send: Send
 

Originally posted by Marilyn de Queiroz:
A class does not need to be public to run.

Regarding text editors, there are many that are much better in my opinion than notepad. Mark named a few. Also, UltraEdit and even DOS's old 'edit' program (at least it maintains the indents). I think that Word and Notepad are two of the worst.



I agree that NotePad is a poor choice for a text editor, although it is slightly better than Word for this purpose. As yet another alternative, you should check out TextPad. It is a great text editor for Windows in my opinion. You can use it "out of the box" as it is. Once you get more comfortable with Java and the editor, you can also customize it to help with some of the more mundane tasks that you encounter when programming in Java.

Good luck and don't give up

Layne
+Pie Number of slices to send: Send
 

Originally posted by Mark Spritzler:
When the class was not public, and I ran java, it gave the error, when I added public to the class, then it would run through java.


Strange I've never encountered that.
+Pie Number of slices to send: Send
 

Originally posted by Marilyn de Queiroz:

Strange I've never encountered that.



I just think it is my machine doing a practical joke on me. It works fine now. No need for public.

Mark
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1723 times.
Similar Threads
Book Answer vs JVM, Same Code Different Output?
Stuck on something with arrays
Be the compiler excercise in chap 3
Head First Java Code Magnets Chapter 3 Q
Code not working
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:56:32.