• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If, Else If, Else - Truth Expressions

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I did write an intro Java book. Just not a very good one!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another great book would be Head First 2nd Edition
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic