@@ -7,9 +7,10 @@ previous-page: taste-intro
77next-page : taste-repl
88---
99
10+ ## Your First Scala Program
1011
1112A 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
2223Next, compile the code with ` scalac ` :
2324
2425``` bash
25- $ scalac Hello .scala
26+ $ scalac hello .scala
2627```
2728
2829If 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
3637hello.class
3738hello.tasty
3839```
@@ -50,6 +51,69 @@ 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+ println(" Please enter your name:" )
72+ val name = readLine()
73+
74+ println(" Hello, " + name + " !" )
75+ ```
5476
77+ In this code we save the result of ` readLine ` to a variable called ` name ` , we then
78+ use the ` + ` operator on strings to join ` "Hello, " ` with ` name ` and ` "!" ` , making one single string value.
5579
80+ > You can learn more about using ` val ` by reading [ Variables and Data Types] ( /scala3/book/taste-vars-data-types.html ) .
81+
82+ Then compile the code with ` scalac ` :
83+
84+ ``` bash
85+ $ scalac helloInteractive.scala
86+ ```
87+ Then run it with ` scala helloInteractive ` , this time the program will pause after asking for your name,
88+ and wait until you type a name and press return on the keyboard, looking like this:
89+
90+ ``` bash
91+ $ scala helloInteractive
92+ Please enter your name:
93+ ▌
94+ ```
95+
96+ When you enter your name at the prompt, the final interaction should look like this:
97+
98+ ``` bash
99+ $ scala helloInteractive
100+ Please enter your name:
101+ Alvin Alexander
102+ Hello, Alvin Alexander!
103+ ```
104+
105+ ### A Note about Imports
106+
107+ As you saw in this application, sometimes certain methods, or other kinds of definitions that we'll see later,
108+ are not available unless you use an ` import ` clause like so:
109+
110+ ``` scala
111+ import scala .io .StdIn .readLine
112+ ```
113+
114+ Imports help you write code in a few ways:
115+ - you can put code in multiple files, to help avoid clutter, and to help navigate large projects.
116+ - you can use a code library, perhaps written by someone else, that has useful functionality
117+ - you can know where a certain definition comes from (especially if it was not written in the current file).
118+
119+ [ scala_tools] : {% link _ overviews/scala3-book/scala-tools.md %}
0 commit comments