File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If, Else If, Else - Truth Expressions

 
Ben Johnson
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ben Johnson
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I did write an intro Java book. Just not a very good one!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Megs Maquito
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another great book would be Head First 2nd Edition
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic