• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

First days of Java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Marshal
Posts: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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).
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice! Thanks for the (super) fast reply!

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


Works beautifully!

 
Campbell Ritchie
Marshal
Posts: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76798
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Greenhorn
Posts: 7
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic