Skip to content

Commit

Permalink
add command-line-io to hello-world
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Jun 22, 2022
1 parent ad64bd4 commit b0ffd87
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion _overviews/scala-book/command-line-io.md
Expand Up @@ -9,7 +9,7 @@ num: 12
outof: 54
previous-page: two-notes-about-strings
next-page: control-structures
new-version: /scala3/book/introduction.html
new-version: /scala3/book/taste-hello-world.html#ask-for-user-input
---


Expand Down
73 changes: 66 additions & 7 deletions _overviews/scala3-book/taste-hello-world.md
Expand Up @@ -7,9 +7,10 @@ previous-page: taste-intro
next-page: taste-repl
---

## Your First Scala Program

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

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

```bash
$ scalac Hello.scala
$ scalac hello.scala
```

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

```bash
$ ls -1
Hello$package$.class
Hello$package.class
Hello$package.tasty
Hello.scala
hello$package$.class
hello$package.class
hello$package.tasty
hello.scala
hello.class
hello.tasty
```
Expand All @@ -50,6 +51,64 @@ Assuming that worked, congratulations, you just compiled and ran your first Scal

> More information about sbt and other tools that make Scala development easier can be found in the [Scala Tools][scala_tools] chapter.
[scala_tools]: {% link _overviews/scala3-book/scala-tools.md %}
## Ask For User Input

In our next example let's ask for the user's name before we greet them!

There are several ways to read input from a command-line, but a simple way is to use the
`readLine` method in the _scala.io.StdIn_ object. To use it, you need to first import it, like this:

```scala
import scala.io.StdIn.readLine
```

To demonstrate how this works, let’s create a little example. Put this source code in a file named _helloInteractive.scala_:

```scala
import scala.io.StdIn.readLine

@main def helloInteractive() =
print("Please enter your name: ")
val name = readLine()

println("Hello, " + name + "!")
```

> You can learn more about using `val` by reading [Variables and Data Types](/scala3/book/taste-vars-data-types.html).
Then compile it with `scalac`:

```bash
$ scalac helloInteractive.scala
```
Then run it with `scala helloInteractive`, this time the program will pause after asking for your name,
and wait until you do, looking like this:

```bash
$ scala helloInteractive
Please enter your name: ▌
```

When you enter your name at the prompt, the final interaction should look like this:

```bash
$ scala helloInteractive
Please enter your name: Alvin Alexander
Hello, Alvin Alexander!
```

### A Note about Imports

As you saw in this application, sometimes certain methods, or other kinds of definitions that we'll see later,
are not available unless you use an `import` clause like so:

```scala
import scala.io.StdIn.readLine
```

Imports help you write code in a few ways:
- you can put code in multiple files, to help avoid clutter, and to help navigate large projects.
- you can use a code library, perhaps written by someone else, that has useful functionality
- you can know where a certain definition comes from (especially if it was not written in the current file).

[scala_tools]: {% link _overviews/scala3-book/scala-tools.md %}

0 comments on commit b0ffd87

Please sign in to comment.