Skip to content

Commit b0ffd87

Browse files
committed
add command-line-io to hello-world
1 parent ad64bd4 commit b0ffd87

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

_overviews/scala-book/command-line-io.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ num: 12
99
outof: 54
1010
previous-page: two-notes-about-strings
1111
next-page: control-structures
12-
new-version: /scala3/book/introduction.html
12+
new-version: /scala3/book/taste-hello-world.html#ask-for-user-input
1313
---
1414

1515

_overviews/scala3-book/taste-hello-world.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ previous-page: taste-intro
77
next-page: taste-repl
88
---
99

10+
## Your First Scala Program
1011

1112
A Scala 3 “Hello, world!” example goes as follows.
12-
First, put this code in a file named _Hello.scala_:
13+
First, put this code in a file named _hello.scala_:
1314

1415
```scala
1516
@main def hello() = println("Hello, world!")
@@ -22,17 +23,17 @@ It prints the `"Hello, world!"` string to standard output (STDOUT) using the `pr
2223
Next, compile the code with `scalac`:
2324

2425
```bash
25-
$ scalac Hello.scala
26+
$ scalac hello.scala
2627
```
2728

2829
If you’re coming to Scala from Java, `scalac` is just like `javac`, so that command creates several files:
2930

3031
```bash
3132
$ ls -1
32-
Hello$package$.class
33-
Hello$package.class
34-
Hello$package.tasty
35-
Hello.scala
33+
hello$package$.class
34+
hello$package.class
35+
hello$package.tasty
36+
hello.scala
3637
hello.class
3738
hello.tasty
3839
```
@@ -50,6 +51,64 @@ Assuming that worked, congratulations, you just compiled and ran your first Scal
5051

5152
> More information about sbt and other tools that make Scala development easier can be found in the [Scala Tools][scala_tools] chapter.
5253
53-
[scala_tools]: {% link _overviews/scala3-book/scala-tools.md %}
54+
## Ask For User Input
55+
56+
In our next example let's ask for the user's name before we greet them!
57+
58+
There are several ways to read input from a command-line, but a simple way is to use the
59+
`readLine` method in the _scala.io.StdIn_ object. To use it, you need to first import it, like this:
60+
61+
```scala
62+
import scala.io.StdIn.readLine
63+
```
64+
65+
To demonstrate how this works, let’s create a little example. Put this source code in a file named _helloInteractive.scala_:
66+
67+
```scala
68+
import scala.io.StdIn.readLine
69+
70+
@main def helloInteractive() =
71+
print("Please enter your name: ")
72+
val name = readLine()
73+
74+
println("Hello, " + name + "!")
75+
```
76+
77+
> You can learn more about using `val` by reading [Variables and Data Types](/scala3/book/taste-vars-data-types.html).
78+
79+
Then compile it with `scalac`:
80+
81+
```bash
82+
$ scalac helloInteractive.scala
83+
```
84+
Then run it with `scala helloInteractive`, this time the program will pause after asking for your name,
85+
and wait until you do, looking like this:
86+
87+
```bash
88+
$ scala helloInteractive
89+
Please enter your name: ▌
90+
```
91+
92+
When you enter your name at the prompt, the final interaction should look like this:
5493

94+
```bash
95+
$ scala helloInteractive
96+
Please enter your name: Alvin Alexander
97+
Hello, Alvin Alexander!
98+
```
5599

100+
### A Note about Imports
101+
102+
As you saw in this application, sometimes certain methods, or other kinds of definitions that we'll see later,
103+
are not available unless you use an `import` clause like so:
104+
105+
```scala
106+
import scala.io.StdIn.readLine
107+
```
108+
109+
Imports help you write code in a few ways:
110+
- you can put code in multiple files, to help avoid clutter, and to help navigate large projects.
111+
- you can use a code library, perhaps written by someone else, that has useful functionality
112+
- you can know where a certain definition comes from (especially if it was not written in the current file).
113+
114+
[scala_tools]: {% link _overviews/scala3-book/scala-tools.md %}

0 commit comments

Comments
 (0)