java - Simple conditional on scanned input -
this question has answer here:
- how compare strings in java? 23 answers
i've written simple program check if input letter o or not. reason, when type letter o, program outputs input not letter o. have used eclipse debugger ensure input variable equals "o".
import java.util.scanner; public class scannertest { public static void main(string[] args) { scanner scan = new scanner(system.in); system.out.println("give input. tell if input 'o' or not"); string input = scan.next(); if (input == "o"){ system.out.println("your input 'o'"); } else { system.out.println("your input not 'o'"); } } }
you need use equals
method instead of ==
operator, this:
if(input.equals("o"))
using ==
compares memory address of string objects instead of values in order ==
return true
have comparing same string object.
Comments
Post a Comment