Stop scanner from reading user input java?

前端 未结 4 647
走了就别回头了
走了就别回头了 2021-01-15 14:35

I am trying to write this method, which keeps reading from the user, until the word \"exit\" is inputted. I tried with a break and for loop; it didn\'t work. I was trying wi

4条回答
  •  自闭症患者
    2021-01-15 15:12

    name == exit is wrong.

    You want

    name.equals(exit)
    

    or

    name.equals("exit")
    

    depending on whether exit is a variable or a string literal, respectively. In Java, == means reference equality (e.g Are these two references pointing to the same address in memory), whereas .equals means object equivalence, which is typically overriden in the class by the developer.

提交回复
热议问题