hi i have a doubt i did this coding and when i complied it and tried to run the program evenupon giving the correct answer("ritchie") the output i got says answer is wrong.
can anyone tell me what is wrong with the coding

import java.io.*;
class Invent
{
  public static void main(String args[])
{

 try
{
  BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("who is the inventor of c :");
  System.out.flush();
  String answer = d.readLine();
if(answer != "ritchie")
  {
    System.out.println("answer is wrong");
  }
else
{
  System.out.println("answer is correct"); 
 }
}
catch(IOException e)
 {
  System.out.println("wrong input");
 }
}
}

Do not use "=" or "!=" to compare strings. Use the string function equals() or equalsIgnoreCase().

thank you EZZARAL i tried that and it works now.

or you can try this

if(answer == "ritchie")
  {
    System.out.println("answer is correct");
  }
else
{
  System.out.println("wrong answer"); 
 }

No, actually you can't. Read my post above. You dredged up an old solved thread to post a "solution" that is exactly the same as the original problem.

Don't use "==" for string comparison.

ok.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.