Information Sheet User Inputs
Information Sheet User Inputs
Department of Education
COMPUTER PROGRAMMING 10
INFORMATION SHEET
USER INPUTS IN JAVA
The Scanner class allows us to read primitive data types and Strings from
standard input (which is the keyboard). The standard input stream is System.in.
The Scanner class is found in the java.util package. Unlike java.lang., the
java.util package is not automatically visible to your Java program. We have two
options when using the Scanner class because of this.
1. You can place import java.util.Scanner; at the start of your .java file before
your class declaration. This allows us to use the Scanner class in our class
by simply referring to the class name as we would with the String class,
for example. Thus, Scanner input = new Scanner( System.in ); will create
a Scanner object (that we called input)
2. Or, you can use the full path for the class, java.util.Scanner, each time we
need to reference the class. In this case,
java.util.Scanner input = new java.util.Scanner( System.in ); will create a
Scanner object called input.
Input Types
In using the scanner, we can either import the Scanner class before the class
declaration or declare the full path every time we need the scanner class. To
illustrate the 2 methods, let’s take a look at the following examples. Both will
display the same result but the method to declare the scanner class is different.
Example #1:
Laptop #1 – 10
Write a program that will ask the user for the following data: Name, Age, and
Address and display the inputted data of the user and print if the user is eligible
to vote or not based on the rule that all 18 years old and above can vote.
Laptop #11 - 20
Write a Temperature Converter Program that will ask the user for a temperature
and its unit (Celsius or Fahrenheit) and convert it to other temperature unit
using the formula:
Given:
Formula to Convert Celsius to Fahrenheit: F = (C * 9/5) + 32
Formula to Convert Fahrenheit to Celsius: C = (F - 32) * 5/9
Laptop #21 - 30
Write a Grocery Billing program that will ask the user for 5 item names, prices,
and quantities and compute and display the total bill.
Laptop #31 - 40
Write a Login System Simulation program that ask for a username and
password. If the username is "admin" and the password is "1234", print
"Access Granted"; otherwise, print "Access Denied".
Make sure to write a comment explaining each code blocks. Points will be
deducted if you failed to explain the code thru comment.
Prepared by: