import java.util.Scanner;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter your name: ");
Method name | What it does |
---|---|
next() | gets the next token in the input stream as a String |
nextInt() | gets the next token in the input stream as a int |
nextDouble() | gets the next token in the input stream as a double |
nextLine() | gets the entire line in the input stream |
Record your participation in Piazza clicker question Lesson8 Q1
Java Factory is having a sale where everything is being sold at 2/3 of its original cost. Write a program to read price from the keyboard and then calculate and print the new price (Call the new price discountedPrice). Do not use .67. Let Java do the division to get a more acurrate answer
What do you need to do? Here is pseudocode
create a Scanner prompt for a price get the next double from the keyboard calculate 2/3 of the price print it
Here is the skeleton of a project you can use for getting input
Record your participation in Piazza clicker question Lesson8 Q2
Our boss is not happy with the way output looks in the last activity. It looks messy. The boss wants us to modify the previous program to format the discountedPrice with a dollar sign and 2 decimal places. Which of the following statements will do that?
Record your participation in Piazza clicker question Lesson8 Q3
Look at this string:
String city = "San Jose";
S | a | n | J | o | s | e | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Often you want just a portion of a string, called a substring. To get a substring, use the following code
str.substring(start, pastEnd)
Given this string:
String str = "Udacity";
U | d | a | c | i | t | y |
0 | 1 | 2 | 3 | 4 | 5 | 6 |
look at these substring examples:
substring | code to extract the substring |
---|---|
U | str.substring(0,1) str.charAt(0) |
y | str.substring(6) str.substring(str.length() -1) str.charAt(6) str.charAt(str.length() -1) |
Uda | str.substring(0, 3) |
city | str.substring(3,7) str.substring(3) |
Record your participation in Piazza clicker question Lesson8 Q4
Given this declaration:
String word = "University";
What is returned by word.substring(1, 5);
Record your participation in Piazza clicker question Lesson8 Q5
Now assume we have a string of unknown length called text. We know it contains more than one word.
Record your participation in Piazza clicker question Lesson8 Q6
Write a program that asked the user to enter a String from the keyboard. Print the string and then print the first and last characters. Test using the Strings hello and San Jose
Here is the skeleton of a project to get input
Record your participation in Piazza clicker question Lesson8 Q7
Write a program that asked the user to enter a phrase of 3 words. Print the phrase and then print just the first word and finally print the length of the first word.
The first word is the letters up to the space. You can find the position of the space by using this statement
int indexOfSpace = pharse.indexOf(" ");