+ 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

16th Dec 2017, 4:31 PM
#RahulVerma
#RahulVerma - avatar
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
16th Dec 2017, 5:10 PM
D_Stark
D_Stark - avatar
+ 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.
16th Dec 2017, 4:36 PM
Hrishikesh Kulkarni
Hrishikesh Kulkarni - avatar
+ 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.
16th Dec 2017, 4:36 PM
Sad
Sad - avatar
18th Dec 2017, 12:16 PM
Rishabh Agrawal
Rishabh Agrawal - avatar