• 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

A Problem with the if statements

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Everyone, So, i tried making a simple calculator with java which should be a very easy job but as it turns out i am being held off with a problem , i have imported the scanner and defined my variables . everything runs okay but when i choose my operation it directly goes into the else statement and ignores the if and else if statements . i tried what i can to fix the problem but it is still the same .
please can you tell where i got it wrong ?

 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

Don't use the == operator to compare Strings (or any Object references) for equality. It doesn't compare equality, but rather identity. It returns true only if both references are pointing to the same object, not if the objects contain the same data.

Instead, use:

 
Sultan -Ahmed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:Hi.

Don't use the == operator to compare Strings (or any Object references) for equality. It doesn't compare equality, but rather identity. It returns true only if both references are pointing to the same object, not if the objects contain the same data.

Instead, use:



Thanks a lot for the answer, i'll keep that in mind ,
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also use the switch statement.
 
Greenhorn
Posts: 11
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also, .equals() is a String function, meaning it can only be used to compare strings to other strings, except, of course, if you convert it first!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually .equals(...) is not a String function, it is declared in Object. You can compare any two Objects using .equals(...) and it will compile.

However a String will never equal an object that is also not a String, so the method will return false in that case.
 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:You could also use the switch statement.



What is the hurry???

We had to fix code lately because of switch on strings statements and some smart developer had its compliance settings to 1.7 or 1.8 and the result was that the code wouldn't run in our client environment if we had released it

Use constructs that work everywhere. It always has been at the core of the java backward compatibility mentality.

It is fine to use the latest features if you control the environment or if you are just experimenting but please stop encouraging the Greenhorns to use the latest features as soon as you get a chance, like I see often done here.

A greenhorn should start learning java with 1.0 although in 2015, I would probably settle for 1.4. Prompting them to use the latest nifty features is not in their interests IMHO, especially if they walk into their first job interview and the interviewer realizes the candidate wouldn't take off fast enough in a java 1.5 environment because he had followed too much coderanch advice ;-)

Worst, the mention "since java 1.X" is usually completely absent from most of these posts. At least, if a clear warning about the compatibility issues was given in those posts, maybe I could go for it.

But in the end, it is as I said above; a greenhorn should start with 1.0-1.4 and master that first. Then, learn the nifty latest features as he goes.

Just my 2 cents..

Cheers,
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:You could also use the switch statement.



Hello,

I have just visited the link you provided in your post and I searched for the strings "since" and "1.7" and neither string seem to be present in the document.

My work environment in java 1.6, would this type of construct work in my environment ?

Thanks in advance!

P.S. Please create a "since 1.X" tag/flags/field for the posts, it would be a really nifty feature ;-)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The Java™ Tutorials paged linked to wrote:Using Strings in switch Statements

In Java SE 7 and later

I would suggest that learning a pre‑Java5 programming style might be harmful; you would learn Collections without generics and then have to unlearn it to learn generics.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

The Java™ Tutorials paged linked to wrote:Using Strings in switch Statements

In Java SE 7 and later

I would suggest that learning a pre‑Java5 programming style might be harmful; you would learn Collections without generics and then have to unlearn it to learn generics.



OK, I get it, it's like; it would be harmful to learn how you were born and where you came from. You only have to learn what you have become. How can this make sense?

I am sorry Campbell, maybe I am just from an old school of taught; learning history about a programming language is important otherwise you never really get the feeling of said language. Of course, if one intent was to produce coding monkeys then, your point could be considered indeed ;-)
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't mean not to learn the history. There is a difference between learning programming and learning a language.

Wert thou to come to me to ask I instruct thee in the English tongue, wert thou grateful I taught thee thus?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I didn't mean not to learn the history. There is a difference between learning programming and learning a language.

Wert thou to come to me to ask I instruct thee in the English tongue, wert thou grateful I taught thee thus?



;-)
 
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
But you are right about versions. In fact I do try to say Java7+ or Java8 only in my posts. That is a useful reminder to our users. I no longer say Java5+ (usually) because Java5 is now over 11 years old.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

The Java™ Tutorials paged linked to wrote:Using Strings in switch Statements

In Java SE 7 and later

I would suggest that learning a pre‑Java5 programming style might be harmful; you would learn Collections without generics and then have to unlearn it to learn generics.



Campbell,

By the way, Oracle seems to be be diverging or careless about the original convention. It should be "Since Java SE 7 and later" not; '"In Java SE 7 and later".

Also, I encourage everybody to have look at the @since javadoc tag. You can even use it in your own custom libraries.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic