• Post Reply Bookmark Topic Watch Topic
  • New Topic

First days of Java  RSS feed

 
Theodore Kall
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

Two days into Java, trying to get familiar with the new lingo... Confusing, but am sure it will make sense eventually!

I had in mind to create a simple program, based on the few bits I learnt online. But for some reason, I am not getting the right result.

Where is my mistake? Can someone point it out?

 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The one problem I can see is using the == operator on reference types. It might work in those circumstance in other languages (e.g. C#) but it won't work in Java®. Look at our FAQ.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore Kall wrote:
Where is my mistake? Can someone point it out?


Welcome to the Ranch.
1) Does your code compile?
2) What is the expected behaviour?
3) How does it differ from the observed behaviour?

Please TellTheDetails
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

First, remember that ItDoesntWorkIsUseless (this is a link).
Second, read this and you'll find the answer: AvoidTheEqualityOperator (also a link).
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

In general, it's better to ask a specific question rather "I am not getting the right result" (TellTheDetails <- that's a link). But here I can see what is probably the problem.

You shouldn't use the == operator with Strings. Use the equals() method.

And since name could be null, it's safer to write
 
Theodore Kall
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice! Thanks for the (super) fast reply!

Got it, no "==", ".equal()" instead!


Works beautifully!

 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing. That use of the Scanner#nextLine() method. In the circumstances you have it, it is absolutely all right and will work correctly. But tell us what your book says about nextLine. Does it tell you there are circumstances when it can return an empty String? When you get an empty String back from nextLine, look here for an explanation (and a simple solution).
 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . .
And since name could be null, . . .
Not off a Scanner. You can get all sorts of naughty things from nextLine, e.g. empty line, no such element exception, but you can't get null.  I have tried it. "Theo".equals(myText) is always a good idea, but in this circumstance there is no risk of a null pointer exception.
 
Theodore Kall
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I love the "Theo".equals(name) idea...
It makes absolute sense.

I need to figure out this Scanner thing...
It looks like there is so many methods in there to learn! One by one!

Campbell Ritchie wrote: But tell us what your book says about nextLine.


I am learning from some online tutorials at the moment... They are not super detailed in their explanations but will invest in a book soon enough.
 
Campbell Ritchie
Marshal
Posts: 55774
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not used that tutorial, but Udemy have a good reputation. I can tell you how many methods you should learn off by heart:-
equals
hashCode (later)
toString
System.out.println
System.out.printf (later)
Once you have learnt those five, you can look up all the other 834658365936 of them in the API documentation. System.out.printXXX are methods of the out field of the System class and you can look them up in the documentation.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ever want to compare two Strings say string1 and string2, remember you always have to use .equals() method.

Like

if(string1.equals(string2))
{
System.out.println("equal strings");
else
System.out.println("not equal");
 
Gisele Mariana
Greenhorn
Posts: 7
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read somewhere that "==" tests object references, And ".equals()" on the other hand tests the string values.
So its better use ".equals()" but also it causes exceptions on calling from a null string.
But anyways using ".equals()" is better with strings
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!