0

Just looking at past exam papers and stumbled across the following question which doesn't make much sense to me:

The following piece of code is meant to greet the user in an appropriate manner:

public static void main( String[] args ) {
    System.out.println("Enter a name");
    String name = EasyIn.getString();
    if (name == "" ) 
        System.out.println("You must input a name");
    else 
        System.out.println("Hello, " + name);
}

The question asks why the program may not do what the programmer intended. With a lack of a laptop at the moment (spilt drink on it!) I can't test the code with different inputs etc. From what I can tell, the only problem with it is that it doesn't ask the user for another input of name if it is blank, and just terminates the program. I'd use this as the answer, but it is brought up later on in the question directly which makes me think it isn't the answer they're looking for. Are there any other problems with the code?

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
Alex Blundell
  • 2,084
  • 5
  • 22
  • 31

9 Answers9

2

Another String.equals question, replace:

if (name == "" )

with

if (name.equals(""))

The == operator compares object references, You need to use String.equals to compare String content. Alternatively you could use:

if (name.IsEmpty())
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Change:

if (name == "" )

to:

if (name.equals(""))
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

It uses == "" instead of .equals(""), or better yet, .isEmpty().

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

Use String#equals(), not ==, for checking string equality.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

The only problem is String comparison using == operator. That would not give the intended result. == operator compares the String Reference values, and not the contents.

String comparison should always be done using equals method, if the intention is to compare the contents of the strings.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

You should check string equality using equals() method. == operator in case of strings just checks if two strings refer to the same string object

 if (name == "" )

should be

 if (name.equals("") )

or you could also use String.isEmpty() to check if the string isempty

 if (name.isEmpty())
PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

It compares Strings with == instead of .equals().

== compares object references. It doesn't compere the contents of two strings.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

the problem is, that you can not do == with strings. You must use the method name.equals

Alexei
  • 347
  • 2
  • 10
0

Sorry, I can't to leave the comments. 1. So, what you meant when wrote that program just is terminates? I think, it should print one of two "println()" - which? 2. Mybee you should insert something sorta loop "while", that will operate until user enters not empty string:

while (!name.equals("")) { ... }

PS. sorry, I cannot to leave the comments & I saw that there isn't any approved answer...

Dedyshka
  • 421
  • 3
  • 10
  • 20