Chapter 3a - User Input
Scannerโ
Scanning for User Inputโ
import java.util.*;
class Main {
public static void main(String arg[]) {
System.out.print("Enter Your Name : "); // user prompt
Scanner sc = new Scanner(System.in); // take user input
String name = sc.nextLine(); // store the user input in the name variable
System.out.println("Name : "+ name); // output the value stored in name
}
}
๐งช Try the code out~!
๐โโ๏ธ What's also imported from Java Util?
Here is a list of al the things we are importing when we write:
import java.util.*
https://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html
Methods for accepting user Inputโ
Code | What is used for |
---|---|
nextInt() | It is used to take an integer as an input. |
nextFloat() | It is used to take float as an input. |
nextDouble() | It is used to take double as an input. |
nextLine() | It is used to take String as an input(It will accept a line). |
nextBoolean() | It is used to take the boolean value as an input. |
nextLong() | It is used to take long as an input. |
Asking for a char
- The following line will save the first letter on the next line
- To do that we are using charAt(0) which means
The character that is on the index 0 (we start counting from 0 as the first letter)
char character = sc.nextLine().charAt(0);
Number Inputsโ
๐ Lesson 3 Learning Activities [E1] : Prediction with User Inputs in Java
Example: Bake Store Program
In this example we create a Program for a Bake Bar that helps the store clerk into calculating the total cost of the items the customer purchases.
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("------------------------------------------------");
System.out.println("The following items are availabe at Bake Bar: ");
System.out.println("Shortcakes at $1.5 per cake");
System.out.println("Macaron at $1 per piece");
System.out.println("Chocochip cookies at $1 per cookie");
System.out.println("-------------------------------------------------");
System.out.print("Enter the number of shortcakes you want: ");
int shortcake = scan.nextInt();
System.out.print("Enter the number of macarons you want: ");
int macaron = scan.nextInt();
System.out.print("Enter the number of cookies you want: ");
int cookie = scan.nextInt();
double costCake = 1.5 * shortcake; //calculate the money spent on shortcake
double costMacaron = 1 * macaron; // calculate the money spent on macarons
double costCookie = 1 * cookie; // calculate the money spent on cookies
double totalCost = costCake + costMacaron + costCookie;
//calculate the total money spent on all 3 items
System.out.println("Bill amount for your shopping is $" + totalCost);
}
}
๐งช Try the code out!
No need to store inputs
The following code works just fine too!
System.out.println("Enter Your Name");
Scanner sc=new Scanner(System.in);
System.out.println(sc.nextLine());
Create a Madlibโ
Create the following Program
- Take user inputs for words to fill in the blanks.
- Be sure to provide appropriate prompts to let your user know what she/he should be entering.
- Print out the completed madlib using string concatenation.
- You may store the text blocks for your madlib in variables or use them directly as strings for concatenation when you output.
Sample Program
โ Solve the problem using Replit
โ You can solve the problem here using Trinket
Steps
- Complete The following code so that it scans for the noun and prints the noun entered.
- Complete the code so that it also asks for the adverb. Feel free to uncomment line 13
- Complete the code so that it also ask the verb. Feel free to uncomment line 14
- Scan and print the adjective. Feel free to uncomment line 15
Exercise: Improving the Bake Shopโ
- The following code only have the following menu items on it.
Item | Price |
---|---|
Cake | $1.5 |
Macaron | $1 |
Cookie | $1 |
- ๐จ Modify this code so that now you have a menu itme available: e.g.:
Item | Price |
---|---|
Bread | $5 |
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("------------------------------------------------");
System.out.println("The following items are availabe at Bake Bar: ");
System.out.println("Shortcakes at $1.5 per cake");
System.out.println("Macaron at $1 per piece");
System.out.println("Chocochip cookies at $1 per cookie");
System.out.println("-------------------------------------------------");
System.out.print("Enter the number of shortcakes you want: ");
int shortcake = scan.nextInt();
System.out.print("Enter the number of macarons you want: ");
int macaron = scan.nextInt();
System.out.print("Enter the number of cookies you want: ");
int cookie = scan.nextInt();
double costCake = 1.5 * shortcake; //calculate the money spent on shortcake
double costMacaron = 1 * macaron; // calculate the money spent on macarons
double costCookie = 1 * cookie; // calculate the money spent on cookies
double totalCost = costCake + costMacaron + costCookie;
//calculate the total money spent on all 3 items
System.out.println("Bill amount for your shopping is $" + totalCost);
}
}