+ 2
In which variable the input of the following code will store?
import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner myVar = new Scanner(System.in); System.out.println(myVar.nextLine()); } }
2 Answers
+ 15
Kushang Shah
You are not storing input in any variable in this code you are just printing whatever user type as input.
So to store input and use in an if else or any other statement u need declare a variable in this case a string variable.
This is a demo how u can do it.
import java.util.Scanner;
class MyClass {
public static void main(String[ ] args) {
Scanner myVar = new Scanner(System.in);
String s = myVar.nextLine();
System.out.println(s);
//write your if statement here like:
if(s.equals("Kushang Shah")){
//Do whatever u want.
}
}
}
Note: if my answer helps u please accept it as correct answer
0
Diego Acero how to use myVar in if...else condition?