I
I
Ivan Actovegin2019-09-17 18:51:12
Java
Ivan Actovegin, 2019-09-17 18:51:12

Why is keyboard input not being validated?

package com.lol;

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  String yep = "shop";
  System.out.print(">");
  String com = in.next();
  if(com == yep){
    System.out.print("Okay, it's work!");
  }else if(com != yep){
    System.out.print("Hey!");
  }else{
    System.out.print("meh...");
  }
 }
}

//After typing from the keyboard, if works strangely, always (Whatever I type, else if works).
Why is this happening ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-09-17
@ivanmorda

Why is this happening?

https://javarush.ru/groups/posts/equals-java-sravn...
if(com.equals(yep)) ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question