-5

I'm writing a simple program where the program asks the user to input some strings and a certain output in generated based on the user's input. But when I run the code the I got some error.Errors I've also tried the scanner import but the same exceptions pop up. And when I moved the imports outside of my main I got 3 different errors again.Errors At this moment I don't need the method to be looped or anything, just want to have it so the program can spit out some output based on user's input. Thanks.

public class Question {
    public static void main(String arg[]) {
        import java.io.BufferReader;
        BufferReader br = new BufferReader(new InputStreamReader(System.in));
        String input = br.readLine("who's your daddy?");

        if (input = "you're my daddy.") {
            System.out.println("correct");
        else {
            System.out.println("try again");
        }
    }
}
}
OH GOD SPIDERS
  • 3,091
  • 2
  • 13
  • 16
aiduaq
  • 29
  • 2
  • 4
  • 1
    import statements cannot be be part of a method or class. They have to be separate at the start of a file. – OH GOD SPIDERS Oct 11 '17 at 08:15
  • See also [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – khelwood Oct 11 '17 at 08:21
  • If you format your code correctly you would see that your braces are messed up: You don't close the brace before your else statement. – OH GOD SPIDERS Oct 11 '17 at 08:23
  • Please copy and paste the error output directly into the question, that makes it easier to read it in one go and for others to find this question based on the error message. – Johannes Müller Oct 11 '17 at 09:06

5 Answers5

1

Edit:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Question {

  public static void main(String[] args) {

    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String input = in.readLine();
      if ("you're my daddy.".equals(input)) {
        System.out.println("correct");
      } else {
        System.out.println("try again");
      }
    } catch (IOException ex) {
      System.out.println("Error reading from System.in");
    }
  }
}

Another hint for String comparisons: It is better to put the String constant in the first place of the comparison to avoid NullPointerExceptions.

if ("you're my daddy.".equals(input)) {
  // ...
}

And a brief explanation of why == is not correct here: This checks if two objects are the same (identity). Every time you write "you're my daddy." a new String is created. Therefore the comparison with == would never be true although the content of the String is identical.

Mitch
  • 79
  • 8
0

Comparisons are achieved with ==, not =. And, in Java, for Strings, you should use the equals() method fot that

input.equals("you're my daddy.") 
Andrea
  • 6,032
  • 2
  • 28
  • 55
0

To compare things, you use ==, not =. That is assignment.

However, input is a string. So you want to use -

if (input.equals("you're my daddy.")) {

Read up about this here - What is the difference between == vs equals() in Java?

Manish Giri
  • 3,562
  • 8
  • 45
  • 81
0

In your code.. you can use buffered reader outside of main like this private static BufferReader br = new BufferReader(new InputStreamReader(System.in));

if (input = "you're my daddy.") { // by using equals method. your code is wrong here

System.out.println("correct");

// not closing curly braces here

0
public class V 
{
   public static void main ( String [] args )
      {

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));          
        System.out.print("Enter your input string: ");

        String input = reader.readLine();
        System.out.println("Your input is: " + input);

        String input1="you're my daddy.";
         if (input.equals(input1))

         {

           System.out.println("correct");
         }
        else 
         {

         System.out.println("try again");

         }

       } 
        catch (IOException e)
        {
        e.printStackTrace();
        }

      } 

} enter code here