• 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

Question on declaration statements and where they go

 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic