+ 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 NgoĢ£c Đăng
NguyĆŖĢƒn NgoĢ£c Đăng - avatar
2 Answers
+ 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 NgoĢ£c Đăng
NguyĆŖĢƒn NgoĢ£c Đăng - avatar