+ 2
[Java]â Which is the best way to take an input?
I started learning basics and when it came to take an input, I was in a total dilemma. Which one of the following is the most appropriate way to take an input: ⢠Scanner ⢠BufferedReader and InputStreamReader ⢠DataInputStream ⢠Console
4 Answers
+ 6
//importing scanner class
Import java.util.Scanner;
//main
//Creating an object from class Scanner
Scanner scn = new Scanner(System.in);
//Assigning scanner to data type int
int i = scn.nextInt();
//or Assigning Scanner to String
String j = scn.nextLine();
//printing input to console
System.out.println(i);
//hope that helps
+ 5
Scanner
next(),nextInt(),nextLine() are there to accept values .
each of the above is used to accept values according to requirement of data types.
+ 4
I use Scanner as I know only that for now. It is easy too. Those nextInt().... are used to read users input. For example, if user needs to input number, you need to use nextInt() as it reads integer values(which are numbers). If user has to input his name, you will use nextLine(). So they are needed for reading users input. I hope I explained clearly.
0
Hey rahul i think this might help you.
https://hackthejava.wordpress.com/2016/09/16/inputoutput-in-java/