Join
the community
Sign-up
Ask a question »

Java- Realine issue

May 2013


Java- Realine issue




Issue


Can someone explain to me why the following program is returning the false value, even when the user enters the correct string "Hello"?

import java.io.*; 

class Test{ 

public static void main(String[] args) throws IOException{ 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
String str = br.readLine(); 
System.out.println(str == "Hello"); 
} 

}

Solution

  • To make a comparison of String must use equals.
  • System.out.println(str.equals("Hello")); 
  • You normally use == to compare references references.


Thanks to Danimo for this tip.

See also

Knowledge communities.

Published by aakai1056
This document entitled « Java- Realine issue » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.
Receive our newsletter

health.kioskea.net

Progress bar in C#
Java -Retrieve current date and time