• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on declaration statements and where they go  RSS feed

 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put a little code snippet below. Code works fine, does everything one expects. But I have a question or two as to how and or why it works the way it does.


Specifically, if I place the String declaration outside the if () it works just fine. But if I place it inside the if() I receive an error: cannot find symbol <nameIn>. I just don't understand why.

The second question is more of a duh kind of thing: I am trying to move away from using == in string comparisons. As I was typing this it dawned on me that it is an integer(or short or byte or something -- I haven't read the documentation yet). I tried to use .equals, but that failed miserably. How would one go about checking if args[0] is a proper string value? Proper in this case being the string value I expect.

Anyway, I hope my question(s) are clear, and any insight would be greatly appreciated.

Robert
[edited to clean up the code. one of the curly brackets was out of place]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:But if I place it inside the if() I receive an error: cannot find symbol <nameIn>. I just don't understand why.

A declaration is limited to the scope in which it is declared. So if you delate it inside the braces for the if scope, it's not available outside of that block (defined by the braces). Therefore, it is undeclared inside the else block.

How would one go about checking if args[0] is a proper string value? Proper in this case being the string value I expect.

args[0] will always be a string, so you would compare it to other values with .equals().
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
A declaration is limited to the scope in which it is declared. So if you delate it inside the braces for the if scope, it's not available outside of that block (defined by the braces). Therefore, it is undeclared inside the else block.

Ah, a duh moment. Cleared that bit right up (I should have caught that myself) Thanks.

args[0] will always be a string, so you would compare it to other values with .equals().

I feel pretty silly, but this just escapes me. I'll have to write a test app and play with this. I tried checking for an empty string, and couldn't get it to work (sorry, I forget the error right now). Probably just another duh moment and a silly syntax error.

Thanks for the reply.

Robert
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:
args[0] will always be a string, so you would compare it to other values with .equals().

I feel pretty silly, but this just escapes me. I'll have to write a test app and play with this. I tried checking for an empty string, and couldn't get it to work (sorry, I forget the error right now). Probably just another duh moment and a silly syntax error.

args[0] will always be a string, but it may be a string that looks like an integer value. "12345" is a string, but can be converted into an int or Integer.

Remember that the user can type ANYTHING on the command line. Java says "we'll treat them all as Strings, because then I know I can handle whatever they type".

If you want to see what is in it, use a lot of System.out.println() statements. print out values. Print out the results of comparisons. Print out "entering if block" and/or "entering else block" to see what the flow of your code REALLY is.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:
args[0] will always be a string, so you would compare it to other values with .equals().
I feel pretty silly, but this just escapes me.

I suspect because you're confusing what a String is with what it contains. Just because it contains "123" does NOT make it a number; it's a value judgement that you've made about the nature of its contents - as you'll soon discover if you print out the result of the expression:
"123" + "456"

If it was a number, you'd get 579, but you don't.

For more information, you might want to read the StringsAreBad page.

HIH

Winston
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:For more information, you might want to read the StringsAreBad page.

Thanks for the link. It was an interesting read, and I gained quite a bit from it. Granted, as an old COBOL programmer (and other procedural languages), thinking in terms of objects is quite different.

Also, I would like to clarify something from my original post. My description of what might be in args wasn't clear, nor what I had intended. When I said it contained a short, byte, int, et. all. I was actually referring to the .length method. I was trying to do something like args.equals(0) I do appreciate all the insights into args.

Robert
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write args.equals(xxx), but you only can get true out of it if args and xxx are the same object. That is because arrays have an un‑overridden equals() method inherited from Object.
If you want equality of content to another array, try this sort of method; there are several other methods which are similar.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The args array contains what is written on the command line after the name of the class whose main method is invoked. If it is written it is writing and writing is implemented as Strings, so the main method's parameter always has the type String[].
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:Thanks for the link. It was an interesting read, and I gained quite a bit from it.

Great. Glad it was of some use.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!