# Using The Scanner for Input Using the Scanner

• Slides: 9

Using The Scanner for Input

Using the Scanner class to make programs more flexible

To use the Scanner class: 1. 2. 3. 4. import the Scanner class instantiate a Scanner object prompt user for input value scan user input into a variable

import java. util. Scanner; import public class CO 2 Calculator 2 { public static void main(String[] args) { int miles. Driven; double mpg; double gallons. Used, co 2 Used; double emissions. Factor = 19. 6; Scanner keyboard = new Scanner(System. in); System. out. print("Enter total miles driven: "); miles. Driven = keyboard. next. Int(); System. out. print("Enter miles per gallon: "); mpg = keyboard. next. Double(); gallons. Used = miles. Driven/mpg; co 2 Used = gallons. Used * emissions. Factor; System. out. println("You used " + co 2 Used + " pounds of C 02”); }

import java. util. Scanner; import public class CO 2 Calculator 2 { public static void main(String[] args) { int miles. Driven; double mpg; double gallons. Used, co 2 Used; double emissions. Factor = 19. 6; Scanner keyboard = new Scanner(System. in); instantiate System. out. print("Enter total miles driven: "); miles. Driven = keyboard. next. Int(); System. out. print("Enter miles per gallon: "); mpg = keyboard. next. Double(); gallons. Used = miles. Driven/mpg; co 2 Used = gallons. Used * emissions. Factor; System. out. println("You used " + co 2 Used + " pounds of C 02”); }

import java. util. Scanner; import public class CO 2 Calculator 2 { public static void main(String[] args) { int miles. Driven; double mpg; double gallons. Used, co 2 Used; double emissions. Factor = 19. 6; Scanner keyboard = new Scanner(System. in); System. out. print("Enter total miles driven: "); instantiate prompt miles. Driven = keyboard. next. Int(); System. out. print("Enter miles per gallon: "); mpg = keyboard. next. Double(); gallons. Used = miles. Driven/mpg; co 2 Used = gallons. Used * emissions. Factor; System. out. println("You used " + co 2 Used + " pounds of C 02”); }

import java. util. Scanner; import public class CO 2 Calculator 2 { public static void main(String[] args) { int miles. Driven; double mpg; double gallons. Used, co 2 Used; double emissions. Factor = 19. 6; Scanner keyboard = new Scanner(System. in); System. out. print("Enter total miles driven: "); miles. Driven = keyboard. next. Int(); instantiate prompt scan System. out. print("Enter miles per gallon: "); mpg = keyboard. next. Double(); gallons. Used = miles. Driven/mpg; co 2 Used = gallons. Used * emissions. Factor; System. out. println("You used " + co 2 Used + " pounds of C 02”); }

More Practice Revise the Java program to calculate carbon emissions so it asks the user for the number of short and medium flights Revised Algorithm ask user for number of short flights taken scan number of short flights (num. Short) ask user for number of medium flights taken scan number of medium flights (num. Medium) calculate total carbon emissions (total. Emissions) num. Short * short. Dist * short. Emission + num. Medium * med. Dist * med. Emission display total carbon emissions Revise CO 2 Air. Travel. java from the previous exercise