• 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

Checking the value of a string

 
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to complete exercise 5-4 of the SCJP study guide and have hit a problem when trying to check the value of the first parameter supplied on the command line when running the program.

I have coded the class thus:

with this code, it does not matter whether the parameter is "Good" or not, the BadFoodException is still thrown..! My first thought is that the if statement is wrong, but it seems OK. Clearly the value "Good" (if supplied) is not being trapped and the else is being executed instead, but why?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just getting started here so I may be wrong.

I think strings are really objects and not primitive values so when you say object == "string" it is always false cause the object reference variable has a pointer in it, a pointer to the actual stirng value on the storage heap.

the equals method should work. x.equals("yourstring")
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know you shouldn't use "==" for String equality. Use the equals method.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You beat me to it, Joe Wolfe; you are quite correct.
 
Stuart Lord
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this (and the help was given in record time - within 15 minutes!!). This is a good example of an elementary mistake, which I'm sure most Java programmers must make at least once..

On a different, but related note, can anyone recommend a Java reference book? I have at least 2 books which methodically teach Java, chapter by chapter, but what would be really useful would be something that simply gives the syntax for a statement, preferably with a worked example. Any suggestions would be very welcome..!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, to answer your question - use String.equals() method. the == operator doesn't do what you expect (as coded)... in that you are posting in beginner, I will defer explaination to other posters.

Originally posted by Stuart Lord:
.... individual items addressed separately ...



Thanks for this (and the help was given in record time - within 15 minutes!!). This is a good example of an elementary mistake, which I'm sure most Java programmers must make at least once..


This place is amazing. If you get saturated mentally, work on your code awhile, then come back in a few days - there are plenty of advanced workers who will address your questions and all of them have made this mistake and others several times. Much of the skill of coding evolves from repeated attempts to write code.

On a different, but related note, can anyone recommend a Java reference book?


We have several authors here. Watch the bottom line of replies. You will end up getting so many books that you need to check out some at the library to avoid collecting books which do not take the approach you want.

I discussed this once with a reference librarian and was told that this is what the inter-library loan program is for, they do it all the time. All they ask is that you check the stacks { :roll: - this term also has a computer science meaning } b4 using ILL.

There is plenty of sample code everywhere, just keep working.

Several people here would be pleased if you try " Head First Java, Second Edition " but bear in mind that you will experience frustrations, and writing one of these beginner books gets some questions and answers that leave an author staring blankly at a wall for a moment before deciding to go do something else.

It's just intrinsic to the work, don't worry about it if you do it. We all do, like your String comparison operator.

I have found that the certification study guides provide the best reference works. The Sun website has sample code, much of it I have used almost directly, ususally pasting it directly into your work needs only the change of a few variable names so that they make sense to you.


[ April 29, 2007: Message edited by: Nicholas Jordan ]
 
Stuart Lord
Ranch Hand
Posts: 40
MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all who have answered my question and to Nicholas for his suggestion for a Java reference book.

Although I am (comparatively) new to Java, I have studied other languages such as COBOL and Pascal and have used them to develop code for real-life applications. What has helped immensely when programming in these languages (and I suspect Java) are books that given an almost-alphabetical list of commands to remind you of how to do something.

For the time, I will stick with the SCJP study guide as a reference source, but thanks for the tip about the Sun website.

Most programmers will have learned to take code and modify it to their own needs and why not? After all why re-invent the wheel..?!
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Lord:
.... are books that given an almost-alphabetical list of commands to remind you of how to do something ....



Study keyword lists, then be sure you know what a parameter list is.

"an almost-alphabetical list of commands"  : would be an abbreviated study guide ~ there isn't any green card in Java that I know of. Get acclimatize to the {}[]() operators and where they are used correctly, plus get used to the notion of a pointer, I don't think there are any pointers per se in Java, but the way the language works you have to be able to think of walking through a collection of data items and working them individually.


This sort of thinking leads to all manner of blunders, which COBOL and PASCAL go to great lengths to isolate the programmers mind from the thinking that results in those blunders, training out the failure mode before it gives Team Leads the heebie-jeebies . Java's answer to this is called, generally, collections and solves a great forest of design issues with a well thought out library of tools for storing and doing operations on data.

Your thinking will evolve faster, because of already being able to write in two linquistics, but the transition will be dramatic because the design-philosophy / paradigm-shift involves moving closer to the actual operation of the machine. I would be interested in your observations during the transition.



Most programmers will have learned to take code and modify it to their own needs and why not? After all why re-invent the wheel..?!



I see you have been reading some books already.

[ May 06, 2007: Message edited by: Nicholas Jordan ]
[ May 13, 2007: Message edited by: Nicholas Jordan ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Wolfe:
Just getting started here so I may be wrong.


All strings are objects in Java, we must differentiate between the declaration of how a class is organized and compare that to what an object is. This distinction is one of those beginner issues that can be difficult to elucidate succinctly yet effectively.

Looks stupid, but it works (for demonstration) ... somewhere, there will be a String.java file - which you can read with an editor - "N:\src\java\lang\String.java" and it will have what a string is in the file, spelled out in Java code.

This comment was taken directly from that file.



I think strings are really objects and not primitive values so when you say object == "string" it is always false cause the object reference variable has a pointer in it, a pointer to the actual string value on the storage heap.



Strings are really objects after you declare one, as given in above sample code. If two Strings are   EXACTLY   the same, the java compiler will collapse them to be the same object, which the == operator takes a highly simplified view of same, returning true if both objects have the same address .... which because they are the same string, is true.

The object reference variable is a pointer, as much as there are pointers in java, but I would avoid using that word because the OO purists will try to move you away from use of that word. " storage heap " is terminology from other linguistics, not used here. Try to use " instances of a class " as it will allow the best acclimatization route.

the equals method should work. x.equals("yourstring")


Yes:

[ May 06, 2007: Message edited by: Nicholas Jordan ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic