Skip to content

Commit 50a32b0

Browse files
committed
Fix up standard input sections
1 parent e0cbd79 commit 50a32b0

File tree

9 files changed

+231
-43
lines changed

9 files changed

+231
-43
lines changed

src/SUMMARY.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,6 @@ BFS
204204
- [Iterate over a String](./loops/iterate_over_a_string.md)
205205
- [Challenges](./loops/challenges.md)
206206

207-
# Interactive Programs II
208-
209-
- [Standard Input II](./standard_input_ii.md)
210-
- [Prompting](./standard_input_ii/prompting.md)
211-
- [Interpreting Input](./standard_input_ii/interpreting_input.md)
212-
- [Reprompting](./standard_input_ii/reprompting.md)
213-
- [Leniency](./standard_input_ii/leniency.md)
214-
- [Delayed Assignment](./standard_input_ii/delayed_assignment.md)
215-
216207
# Projects
217208

218209
<!-- Software is an Interdisciplinary Field -->
@@ -315,11 +306,6 @@ BFS
315306
- [Populate Arrays](./arrays_ii/populate_arrays.md)
316307
- [Challenges](./arrays_ii/challenges.md)
317308

318-
# Projects
319-
320-
<!-- chicken nugget number type things come up when dispensing change -->
321-
- [Point of Sale System](./projects/point_of_sale_system.md)
322-
323309

324310
# Code Structure II
325311

@@ -348,14 +334,13 @@ BFS
348334
- [Clarity](./instance_methods/clarity.md)
349335
- [Challenges](./instance_methods/challenges.md)
350336

351-
# Interactive Programs III
352337

353-
- [Standard Input III](./standard_input_iii.md)
354-
- [Transporting Data](./standard_input_iii/transporting_data.md)
355338

356-
# Projects II
339+
# Projects
340+
341+
<!-- chicken nugget number type things come up when dispensing change -->
342+
- [Point of Sale System](./projects/point_of_sale_system.md)
357343

358-
- [Monte Carlo Sampling]()
359344

360345
# Data Types IV
361346

@@ -395,6 +380,18 @@ BFS
395380
- [null](./switch/null.md)
396381
- [Challenges](./switch/challenges.md)
397382

383+
384+
# Interactive Programs II
385+
386+
- [Standard Input II](./standard_input_ii.md)
387+
- [Reprompting](./standard_input_ii/reprompting.md)
388+
- [Enums](./standard_input_ii/enums.md)
389+
- [Delayed Assignment](./standard_input_ii/delayed_assignment.md)
390+
- [Leniency](./standard_input_ii/leniency.md)
391+
- [Aggregating Data](./standard_input_ii/aggregating_data.md)
392+
- [Challenges](./standard_input_ii/challenges.md)
393+
394+
398395
# Code Structure III
399396

400397
- [Constructors](./constructors.md)

src/standard_input/interpreting_input.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,16 @@ When you call `IO.readln` the human on the other side can type whatever they wan
55
This means that, depending on the question you asked, you might need to interpret
66
what they typed as something other than a `String`.
77

8-
The way to do this differs depending on what type you want, but for at least the types we covered so far
9-
there is a basic-enough process.
8+
In its most basic form this will look like seeing if one `String` equals another `String`.
9+
10+
```java,no_run
11+
void main() {
12+
String color = IO.readln("What is your favorite color? ");
13+
if (color.equals("green")) {
14+
IO.println("Me too!");
15+
}
16+
else {
17+
IO.println("neat.")
18+
}
19+
}
20+
```

src/standard_input_ii.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# Standard Input II
2+
3+
If you are using a program and you type something slightly wrong
4+
it does not feel good if the program then immediately crashes.
5+
6+
As such it often makes sense to not only request your users type something
7+
and then interpret what they typed, but to also give feedback and perhaps ask them again.

src/standard_input_iii/transporting_data.md renamed to src/standard_input_ii/aggregating_data.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Transporting Data
1+
# Aggregating Data
2+
23

34
If you ask someone multiple questions you likely will get multiple variables
45
worth of information.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1
9+
10+
Write a program that asks which Twilight character Bella should have ended up with.
11+
12+
It should keep asking this question until the user types the magic words "i do not care"
13+
14+
```java,no_run
15+
void main() {
16+
// CODE HERE
17+
}
18+
```
19+
20+
## Challenge 2
21+
22+
Update the program above to also accept "I DO NOT CARE", "I Do Not Care", and
23+
any other mix of capital and lower-case letters
24+
25+
26+
## Challenge 3
27+
28+
Write a method called `askForBirthday` that asks for the year, month, and day that someone was born.
29+
This method should return all three pieces of information with a `Birthday` class.
30+
31+
```java,no_run
32+
class Birthday {
33+
// CODE HERE
34+
}
35+
36+
Birthday askForBirthday() {
37+
// CODE ALSO HERE
38+
}
39+
40+
void main() {
41+
Birthday b = askForBirthday();
42+
IO.println(b.year + "-" + b.month + "-" + b.day)
43+
}
44+
```
45+
46+
## Challenge 4
47+
48+
Update the program above to reprompt the user if they enter any nonsensical information
49+
for the year, month, or day. This includes negative numbers for the year, month, or day.
50+
51+
## Challenge 5
52+
53+
Update the birthday program to represent the month with an enum named `Month`.
54+
55+
Accept both the name of the month with any capitalization and the number of the month (starting with `1` for January) as valid ways to specify the month.
56+
57+
Make sure to still reprompt on unexpected inputs.
58+
59+
```java,no_run
60+
enum Month {
61+
JANUARY,
62+
FEBRUARY,
63+
MARCH,
64+
APRIL,
65+
MAY,
66+
JUNE,
67+
JULY,
68+
AUGUST,
69+
SEPTEMBER,
70+
NOVEMBER,
71+
DECEMBER
72+
}
73+
74+
class Birthday {
75+
// CODE HERE
76+
}
77+
78+
Birthday askForBirthday() {
79+
// CODE ALSO HERE
80+
}
81+
82+
void main() {
83+
Birthday b = askForBirthday();
84+
IO.println(b.year + "-" + b.month + "-" + b.day)
85+
}
86+
```

src/standard_input_ii/enums.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Enums
2+
3+
Just as you might want to interpret what someone typed as an `int` or `double`
4+
there are times you will want to interpret input as an `enum` value.
5+
6+
To do this you can write `.valueOf` after the name of the enum. So for a `StopLight` enum `StopLight.valueOf`
7+
can interpret a `String` as a `StopLight`.
8+
9+
```java,no_run
10+
enum StopLight {
11+
RED,
12+
YELLOW,
13+
GREEN
14+
}
15+
16+
void main() {
17+
String colorString = IO.readln("What color was the stoplight? ");
18+
StopLight color = StopLight.valueOf(colorString);
19+
IO.println("The stop light was " + color);
20+
}
21+
```
22+
23+
This will throw an exception if the `String` does not match, so you can reprompt using the same `try`/`catch` structure as you would use with `Integer.parseInt`.
24+
25+
```java,no_run
26+
enum StopLight {
27+
RED,
28+
YELLOW,
29+
GREEN
30+
}
31+
32+
void main() {
33+
StopLight color;
34+
while (true) {
35+
String colorString = IO.readln("What color was the stoplight? ");
36+
try {
37+
color = StopLight.valueOf(colorString);
38+
} catch (RuntimeException e) {
39+
continue;
40+
}
41+
42+
break;
43+
}
44+
45+
IO.println("The stop light was " + color);
46+
}
47+
```
48+
49+
Unfortunately, this only works if what they typed is exactly the name of an enum variant. So
50+
in the example above they need to type `RED` in all capital letters.
51+
52+
To have a different mapping of strings to enum values you need to write code yourself.
53+
54+
```java,no_run
55+
enum StopLight {
56+
RED,
57+
YELLOW,
58+
GREEN
59+
}
60+
61+
StopLight stringToStopLight(String s) {
62+
if (s.equals("r")) {
63+
return StopLight.RED;
64+
}
65+
else if (s.equals("y")) {
66+
return StopLight.YELLOW;
67+
}
68+
else if (s.equals("g")) {
69+
return StopLight.GREEN;
70+
}
71+
else {
72+
throw new RuntimeException("Unknown color.")
73+
}
74+
}
75+
76+
void main() {
77+
String colorString = IO.readln("What color was the stoplight? ");
78+
StopLight color = stringToStopLight(colorString);
79+
IO.println("The stop light was " + color);
80+
}
81+
```
82+

src/standard_input_ii/interpreting_input.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/standard_input_ii/prompting.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/standard_input_ii/reprompting.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,29 @@ void main() {
2727
}
2828
```
2929

30+
If the program would normally crash on unexpected input you can use `try` and `catch` to recover
31+
from this and reprompt the user.
32+
33+
This is applicable to `Integer.parseInt`, `Double.parseDouble`, and any other method that would
34+
throw an exception on unexpected inputs.
35+
36+
```java,no_run
37+
void main() {
38+
int number;
39+
while (true) {
40+
String response = IO.readln("What is your least favorite number? ");
41+
try {
42+
// Here Integer.parseInt might throw an exception,
43+
number = Integer.parseInt(response);
44+
} catch (RuntimeException e) {
45+
// If that happens, go up to the top and reprompt
46+
continue;
47+
}
48+
49+
// If a "continue" is not hit, exit the loop
50+
break;
51+
}
52+
53+
IO.println("Your least favorite number is " + number);
54+
}
55+
```

0 commit comments

Comments
 (0)