Skip to content

Commit 63770df

Browse files
committed
Use IO.readln
1 parent a486cf4 commit 63770df

File tree

8 files changed

+21
-118
lines changed

8 files changed

+21
-118
lines changed

src/global_fields/challenges.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Challenges

src/standard_input.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,3 @@ Programs are pretty boring if they just make a machine warm and do some math.
44

55
There are a lot of ways to make a program interactive, but the easiest is to read
66
from what is called "standard input."
7-
8-
This function will be added to Java in a future version, but until then
9-
copy paste this at the very top of all the code in this section.
10-
11-
```java,no_run
12-
import java.util.Scanner;
13-
14-
Scanner scanner = new Scanner(System.in);
15-
16-
String input(String message) {
17-
System.out.print(message);
18-
return scanner.nextLine();
19-
}
20-
```

src/standard_input/delayed_assignment.md

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,10 @@ One strategy for this is to declare any response-holding variables outside the l
99
The problem with this is that Java isn't smart enough to know that you always initialize those variables.
1010

1111
```java,no_run,does_not_compile
12-
~Scanner scanner = new Scanner(System.in);
13-
~
14-
~String input(String message) {
15-
~ System.out.print(message);
16-
~ return scanner.nextLine();
17-
~}
18-
~
1912
void main() {
2013
String name;
2114
while (true) {
22-
String name = input("What is your name? ");
15+
String name = IO.readln("What is your name? ");
2316
if (name.isBlank()) {
2417
System.out.println("Name cannot be blank!");
2518
continue;
@@ -35,19 +28,10 @@ void main() {
3528
To get around this you can either give an explicit default value.
3629

3730
```java,no_run
38-
~import java.util.Scanner;
39-
~
40-
~Scanner scanner = new Scanner(System.in);
41-
~
42-
~String input(String message) {
43-
~ System.out.print(message);
44-
~ return scanner.nextLine();
45-
~}
46-
~
4731
void main() {
4832
String name = null;
4933
while (true) {
50-
String name = input("What is your name? ");
34+
String name = IO.readln("What is your name? ");
5135
if (name.isBlank()) {
5236
System.out.println("Name cannot be blank!");
5337
continue;
@@ -65,19 +49,10 @@ In this context our old friend delayed assignment becomes an option again. This
6549
to see that the code in the loop will run at least once.
6650

6751
```java,no_run
68-
~import java.util.Scanner;
69-
~
70-
~Scanner scanner = new Scanner(System.in);
71-
~
72-
~String input(String message) {
73-
~ System.out.print(message);
74-
~ return scanner.nextLine();
75-
~}
76-
~
7752
void main() {
7853
String name;
7954
do {
80-
String name = input("What is your name? ");
55+
String name = IO.readln("What is your name? ");
8156
if (name.isBlank()) {
8257
System.out.println("Name cannot be blank!");
8358
continue;

src/standard_input/interpreting_input.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
# Interpreting Input
22

3-
When you call `input` the human on the other side can type whatever they want.
3+
When you call `IO.readln` the human on the other side can type whatever they want.
44

55
This means that, depending on the question you asked, you might need to interpret
66
what they typed as something other than a generic `String`.
77

88
In its most basic form this will look like seeing if one `String` equals another `String`.
99

1010
```java,no_run
11-
~import java.util.Scanner;
12-
~
13-
~Scanner scanner = new Scanner(System.in);
14-
~
15-
~String input(String message) {
16-
~ System.out.print(message);
17-
~ return scanner.nextLine();
18-
~}
19-
~
2011
void main() {
2112
while (true) {
22-
String shouldExit = input("Exit the program? (y/n)");
13+
String shouldExit = IO.readln("Exit the program? (y/n)");
2314
if (shouldExit.equals("y")) {
2415
break;
2516
}

src/standard_input/leniency.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@ This means accounting for common mistakes people make like having extra spaces o
77
For this purpose, methods like `strip` and `equalsIgnoreCase` are useful.
88

99
```java,no_run
10-
~Scanner scanner = new Scanner(System.in);
11-
~
12-
~String input(String message) {
13-
~ System.out.print(message);
14-
~ return scanner.nextLine();
15-
~}
16-
~
1710
void main() {
1811
while (true) {
19-
String response = input("Answer me: yes or no").strip();
12+
String response = IO.readln("Answer me: yes or no").strip();
2013
if (response.equalsIgnoreCase("yes")) {
2114
System.out.println("aight");
2215
}

src/standard_input/prompting.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
# input
22

3-
To prompt a user for information you use the `input` function.
3+
To prompt a user for information you use the `IO.readln` function.
44

5-
The `input` function takes a `String` to output as a prompt. This will
6-
work the same as if the `String` was passed to `System.out.print`.
5+
`IO.readln` takes a `String` to output as a prompt. This will
6+
work the same as if the `String` was passed to `IO.print`.
77

88
The program will then wait until a human types some text and clicks the enter key.
99
Whatever they typed will be returned to the program as a `String`.
1010

1111
```java,no_run
12-
~import java.util.Scanner;
13-
~
14-
~Scanner scanner = new Scanner(System.in);
15-
~
16-
~String input(String message) {
17-
~ System.out.print(message);
18-
~ return scanner.nextLine();
19-
~}
20-
~
2112
void main() {
22-
String name = input("What is your name? ");
13+
String name = IO.scanln("What is your name? ");
2314
System.out.println("Hello, " + name);
2415
}
25-
```
16+
```
17+
18+
`readln` stands for "read line." It reads the next line a person types.

src/standard_input/reprompting.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@ This is a good use case for loops. You ask a question and, if the answer you get
66
you proceed as normal. If it is not then you loop back and ask again.
77

88
```java,no_run
9-
~import java.util.Scanner;
10-
~
11-
~Scanner scanner = new Scanner(System.in);
12-
~
13-
~String input(String message) {
14-
~ System.out.print(message);
15-
~ return scanner.nextLine();
16-
~}
17-
~
189
void main() {
1910
while (true) {
20-
String response = input("Answer me: yes or no");
11+
String response = IO.readln("Answer me: yes or no");
2112
if (response.equals("yes")) {
2213
System.out.println("okay then!");
2314
}

src/standard_input/transporting_data.md

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,9 @@ If you ask someone multiple questions you likely will get multiple variables
44
worth of information.
55

66
```java,no_run
7-
~import java.util.Scanner;
8-
~
9-
~Scanner scanner = new Scanner(System.in);
10-
~
11-
~String input(String message) {
12-
~ System.out.print(message);
13-
~ return scanner.nextLine();
14-
~}
15-
~
167
void main() {
17-
String firstName = input("What is your first name? ");
18-
String lastName = input("What is your last name? ");
8+
String firstName = IO.readln("What is your first name? ");
9+
String lastName = IO.readln("What is your last name? ");
1910
2011
System.out.println("Hello " + firstName + " " + lastName + ".");
2112
}
@@ -25,19 +16,10 @@ This is fine and dandy so long as you immediately use those variables. But once
2516
reprompting logic code can get pretty lengthy.
2617

2718
```java,no_run
28-
~import java.util.Scanner;
29-
~
30-
~Scanner scanner = new Scanner(System.in);
31-
~
32-
~String input(String message) {
33-
~ System.out.print(message);
34-
~ return scanner.nextLine();
35-
~}
36-
~
3719
void main() {
3820
String firstName;
3921
do {
40-
firstName = input("What is your first name? ");
22+
firstName = IO.readln("What is your first name? ");
4123
4224
if (firstName.isBlank()) {
4325
System.out.println("First name cannot be blank.");
@@ -49,7 +31,7 @@ void main() {
4931
5032
String lastName;
5133
do {
52-
lastName = input("What is your first name? ");
34+
lastName = IO.readln("What is your first name? ");
5335
5436
if (lastName.isBlank()) {
5537
System.out.println("First name cannot be blank.");
@@ -69,15 +51,6 @@ I mention all this as a reminder that when you want to return multiple values fr
6951
you can use a class.[^dto]
7052

7153
```java,no_run
72-
~import java.util.Scanner;
73-
~
74-
~Scanner scanner = new Scanner(System.in);
75-
~
76-
~String input(String message) {
77-
~ System.out.print(message);
78-
~ return scanner.nextLine();
79-
~}
80-
~
8154
class Person {
8255
String firstName;
8356
String lastName;
@@ -91,7 +64,7 @@ class Person {
9164
Person askForName() {
9265
String firstName;
9366
do {
94-
firstName = input("What is your first name? ");
67+
firstName = IO.readln("What is your first name? ");
9568
9669
if (firstName.isBlank()) {
9770
System.out.println("First name cannot be blank.");
@@ -103,7 +76,7 @@ Person askForName() {
10376
10477
String lastName;
10578
do {
106-
lastName = input("What is your first name? ");
79+
lastName = IO.readln("What is your first name? ");
10780
10881
if (lastName.isBlank()) {
10982
System.out.println("First name cannot be blank.");

0 commit comments

Comments
 (0)