How to Use Scanner Class in Java with Eclipse IDE: Explanation with Example (Miles Per Gallon Calculation)

in #utopian-io7 years ago (edited)

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:

Portal.url

Now, let's create our example Java file.

Ekran Alıntısı2.PNG

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.

Ekran Alıntısı 3.PNG

Now, in order to create a Scanner object, we need to add the following declaretion to our program:

Ekran Alıntısı 4.PNG

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.

Ekran Alıntısı5.PNG

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.

Ekran Alıntısı6.PNG

Finally divide the covered mile to the used fuel to calculate miles per gallon. So the final code is below.

Ekran Alıntısı7.PNG

When we run the program, program asks the first input:

Ekran Alıntısı8.PNG

Let's say that we covered 150 miles. Enter 150 as input:

Ekran Alıntısı9.PNG

Now the program asks the second input. Let's say that we used 13.2 gallons of fuel. Enter 13,2 as other input:

Ekran Alıntısı10.PNG

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
Portal.url

Step 2: Define scanner
Ekran Alıntısı11.PNG

Step 3: Read your input
Ekran Alıntısı 12.PNG
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

Sort:  

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.

  • The code is not inside code box.
  • The last code box is inaccurate.

You can contact us on Discord.
[utopian-moderator]