We can use the Scanner class to obtain input from the user. To do this, we first import the java.util.Scanner package
import java.util.Scanner;
Then, later in our code, we declare and create a Scanner object in the typical way:
Scanner myScanner = new Scanner(System.in);
Notice in the constructor, we have included "System.in" as a parameter. The constructor requires an input stream be named for the scanner object to watch for input. In this case, "System.in" tells the scanner to watch what the user types in the console window.
Next, (after prompting the user to type something, if appropriate) we can use one of the following methods to actually get our hands on the input typed (depending on the type we want our input to take from then on):
* These are just some of the most common methods used with a Scanner -- one can consult the Java API documentation on the Scanner class for the rest.
So for example, if we wanted the user to type the concentration of a mixture (which will necessarily need to be of type double); and then declare and construct a scanner object to assign the value typed to the (double) variable named "concentration", we could do the following:
System.out.print("Enter the concentration of the mixture: "); Scanner myScanner = Scanner(System.in); double concentration = myScanner.nextDouble();