The scanner class is one of the important classes that exist in java. The Scanner class allows us to read input values. In other worlds, by using Scanner, we can ask the input values to the user of the program.
I will explain the usage of Scanner class by giving gas mileage example which calculates the miles per gallons of fuel to determine the fuel efficiency of the car.
In order to use the Scanner Class, firstly we need to write the following decleration:
Now, let's create our example Java file.
In our example program, we are going to calculate the fuel efficiency of a car by using the number of miles covered and the gallons of fuel used. Therefore, we need to ask these inputs to the user. Let's create the neccessary variables.
Now, in order to create a Scanner object, we need to add the following declaretion to our program:
Now, we need to ask the covered distance to the user. Firstly, we need to write question by regular print comment (first line in following figure). Then, to ask the input to the user, we use the second line in the following figure. Note that, scan.nextInt() is used since the input in the form of integer.
Now, we need to ask the used fuel to the user. Again, we need to write quesiton by regular print comment (first line in following figure). Then, to ask the input to the user, we used the second line in the following figure. Note that, scan.nextDouble() is used since the input in the form of double.
Finally divide the covered mile to the used fuel to calculate miles per gallon. So the final code is below.
When we run the program, program asks the first input:
Let's say that we covered 150 miles. Enter 150 as input:
Now the program asks the second input. Let's say that we used 13.2 gallons of fuel. Enter 13,2 as other input:
As you can see the program calculated MPG by asking the inputs to the user.
Conclusion
To conculude, we can summuraze the scanner class in the following three steps;
Step 1: Import scanner class
Step 2: Define scanner
Step 3: Read your input
Note: Use nextInt for integer inputs, Use nextDouble for double inputs ...
If you want to try this example in your computer, you can find the text version of the code:
import java.util.Scanner;
public class ScannerEx {
public static void main(String[] args) {
int Miles_Covered;
double Gallons_of_Fuel;
double Miles_Per_Gallon;
Scanner scan = new Scanner(System.in);
System.out.print("Please Enter the Distance Covered (Miles):");
Miles_Covered=scan.nextInt();
System.out.print("Please Enter the Fuel Used (Gallons):");
Gallons_of_Fuel=scan.nextDouble();
Miles_Per_Gallon = Miles_Covered / Gallons_of_Fuel;
System.out.println("MilesPerGalon (MPG):"+ Miles_Per_Gallon);
}
}
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.
You can contact us on Discord.
[utopian-moderator]