File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes If, Else If, Else - Truth Expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If, Else If, Else - Truth Expressions" Watch "If, Else If, Else - Truth Expressions" New topic
Author

If, Else If, Else - Truth Expressions

Ben Johnson
Greenhorn

Joined: Jul 12, 2005
Posts: 17
This is probably a stupid question, but I'm sure it won't be the first that's been asked.

Can anyone explain to me why none of these statements are evaluated, and therefore the variable "theUrl" remains an unresolved symbol and throws and exception?

If it matters, the CardNum value will be coming from a form submission, and should therefore be of type String (correct me if I'm wrong).



I would think that the statement enclosed within the else condition would be evaluated no matter what, thereby declaring and initializing theUrl.

What's the deal?

Thanks for your time, it is much appreciated.

Ben
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Ben,

Variable declarations appearing inside any set of curly braces (a "block") are local to that block. You declared three different variables, all with the same name, each local to the block it's defined in.

Note that you can't just remove the braces: a declaration by itself isn't a legal statement and so can't be the body of a conditional.

What you have to do is declare the variable before the conditional, and then tentatively assign to it:



I'm going to move this to Java in General (Beginner), because it's really unrelated to Servlets. You can follow up there.
[ August 01, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Ben Johnson
Greenhorn

Joined: Jul 12, 2005
Posts: 17
Oh, great! Thanks Ernest. I'm not sure how I missed the beginner's forum in the list (though, the list of forums is quite long), but this post definitely belongs here.

Your response makes perfect sense. I'm reading Sun's very own "Core Java 2 - Vol. 1 - Fundamentals", but it never mentions any of the information you just provided (at least nowhere obvious). Makes you wonder who's writing these books and who SHOULD be writing them...

Anyway, thanks a bunch for your help and near-instant reply. You haven't heard the last of me...

Ben
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, I did write an intro Java book. Just not a very good one!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ben Johnson:
... I'm reading Sun's very own "Core Java 2 - Vol. 1 - Fundamentals", but it never mentions any of the information you just provided (at least nowhere obvious)...

I think that's a great book, but not as an introduction to the language. It's a good "second" book to fill in gaps.

You might want to take a look at Bruce Eckel's Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ3.htm

And Sun's Java Tutorial...

http://java.sun.com/docs/books/tutorial/


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
Another great book would be Head First 2nd Edition


I'm a Hood Ornament
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by marc weber:

I think that's a great book, but not as an introduction to the language. It's a good "second" book to fill in gaps.

You might want to take a look at Bruce Eckel's Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ3.htm

And Sun's Java Tutorial...

http://java.sun.com/docs/books/tutorial/


I second this. I think Core Java is a great book. It was my first book on Java. Of course, I was taking a college course, so I had some help from my instructor to fill in any gaps that I was unsure about. I also had plenty of previous programming experience with C++.

I also agree that Core Java is not a good book for beginners. It seems to be geared towards someone who has programmed in at least one other language before.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ben Johnson:
Oh, great! Thanks Ernest. I'm not sure how I missed the beginner's forum in the list (though, the list of forums is quite long), but this post definitely belongs here.

Your response makes perfect sense. I'm reading Sun's very own "Core Java 2 - Vol. 1 - Fundamentals", but it never mentions any of the information you just provided (at least nowhere obvious)....


For what it's worth, I pulled out my copy of Core Java Volume 1. The authors describe the concept discussed here on page 74 of my edition. This probably isn't helpful since I have an old edition; it is probably on a different page in your edition. The discussion of variable scope is in Chapter 3 "Fundamental Programming Structures in Java" at the beginning of the section titled "Control Flow" under the heading "Block Scope". Since this is a crucial concept, I doubt that they cut it out altogether. I'm willing to bet these headings are very similar, if not exactly the same since this hasn't changed since Java 1.0. In case you are interested, this should describe the same concepts that were explained here from a different point of view. Hopefully it will help solidify the whole idea of variable scope.

Keep Coding!

Layne
[ August 05, 2005: Message edited by: Layne Lund ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If, Else If, Else - Truth Expressions