+ 1

Trying to make if statement with string condition, where was i wrong?

import java.util.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String n = input.nextLine(); if (n=="y") {System.out.println("yes");} if (n=="n") {System.out.println("no");} } }

25th Nov 2016, 10:53 AM
Nguyễn Ngọc Đăng
Nguyễn Ngọc Đăng - avatar
2 odpowiedzi
+ 2
you cannot compare like this unless they are from String class make it n= new S tring or you can use n.equals("yes") or you can import libraries that contains methods to equate
25th Nov 2016, 11:02 AM
Sandeep Chatterjee
+ 1
thank you, it worked :)
25th Nov 2016, 11:09 AM
Nguyễn Ngọc Đăng
Nguyễn Ngọc Đăng - avatar