+ 2
why i m not getting the output. I would appreciate any help.
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner a = new Scanner(System.in); String method = a.nextLine(); if(method=="a"){ a(); } else if(method=="b"){ b(); } } public static void a(){ System.out.println("a"); } public static void b(){ System.out.println("b"); } }
3 Réponses
+ 6
Heres the error, replace method=="a" and method == "b" with method.equals("a") and method.equals("b")
That will work, Also there is a LOT of different between .equals and ==
+ 2
@Vishal Prajapati
else if
is proper syntax
@shobhit
.equals
compares the value stored in an object unless you override the method and write your own implementation
==
compares the reference of an object but it works fine on primitives
- 1
here comes the error. else if.
you should correct.
}
else
{
if(method=="b"){
b();
}
Hope it works