0

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");
 }
}
}
3
Contributors
5
Replies
8
Views
9 Years
Discussion Span
Last Post by Apache1
-1

or you can try this

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

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.