0% found this document useful (0 votes)
2 views

Information Sheet User Inputs

This document provides an overview of user input in Java using the Scanner class, detailing how to import it and create objects for reading input from the keyboard. It includes examples of two methods for using the Scanner class and outlines performance tasks for students, such as creating programs for user data input, temperature conversion, grocery billing, and a login system simulation. Additionally, it emphasizes the importance of commenting code for clarity.

Uploaded by

escurelzamantha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Information Sheet User Inputs

This document provides an overview of user input in Java using the Scanner class, detailing how to import it and create objects for reading input from the keyboard. It includes examples of two methods for using the Scanner class and outlines performance tasks for students, such as creating programs for user data input, temperature conversion, grocery billing, and a login system simulation. Additionally, it emphasizes the importance of commenting code for clarity.

Uploaded by

escurelzamantha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Republic of the Philippines

Department of Education
COMPUTER PROGRAMMING 10
INFORMATION SHEET
USER INPUTS IN JAVA

User Input with the Scanner class

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:

import java.util.Scanner; // Import the Scanner class

public class Main {


public static void main(String[] args) {
Scanner Input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username: ");

String userName = Input.nextLine(); // Read user input


System.out.println("Username is: " + userName); // Output user input
}
}
Example #2:

public class Main {


public static void main(String[] args) {
// Create a Scanner object
java.util.Scanner Input = new java.util.Scanner(System.in);
System.out.println("Enter username: ");

String userName = Input.nextLine(); //Read user input


System.out.println("Username is: " + userName); //Output user input
}
}

PERFORMANCE TASK #9 – Java | #8 – Algorithm, Python, & Pascal

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.

Given: Name, Age, Address = User Input

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.

Given: Item Names, Prices, and Quantities = User Input

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".

Given: Username, Password = User Input

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:

Arvin E. Esmeña – TLE Teacher

You might also like