@@ -7,9 +7,10 @@ previous-page: taste-intro
7
7
next-page : taste-repl
8
8
---
9
9
10
+ ## Your First Scala Program
10
11
11
12
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_ :
13
14
14
15
``` scala
15
16
@ main def hello () = println(" Hello, world!" )
@@ -22,17 +23,17 @@ It prints the `"Hello, world!"` string to standard output (STDOUT) using the `pr
22
23
Next, compile the code with ` scalac ` :
23
24
24
25
``` bash
25
- $ scalac Hello .scala
26
+ $ scalac hello .scala
26
27
```
27
28
28
29
If you’re coming to Scala from Java, ` scalac ` is just like ` javac ` , so that command creates several files:
29
30
30
31
``` bash
31
32
$ 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
36
37
hello.class
37
38
hello.tasty
38
39
```
@@ -50,6 +51,64 @@ Assuming that worked, congratulations, you just compiled and ran your first Scal
50
51
51
52
> More information about sbt and other tools that make Scala development easier can be found in the [ Scala Tools] [ scala_tools ] chapter.
52
53
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:
54
93
94
+ ``` bash
95
+ $ scala helloInteractive
96
+ Please enter your name: Alvin Alexander
97
+ Hello, Alvin Alexander!
98
+ ```
55
99
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