java - Simple conditional on scanned input -


this question has answer here:

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

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -