48

I'm currently learning clojure, but I was wondering how to get and store user input in a clojure program. I was looking at the clojure api and I found a function called read-line, however I'm not sure how to use it if it's the right function to use...

Anyhow, how do you get user input in clojure ?

dbr
  • 165,801
  • 69
  • 278
  • 343
JoOb
  • 999
  • 2
  • 10
  • 10
  • BTW - not particularly helpful for read-line - but (doc function) will print the usage and documentation for the function, e.g. (doc read-line). – flatline Jul 13 '09 at 12:59

3 Answers3

65

read-line is the correct function..

(println (read-line))

..would basically echo the users input:

Clojure 1.0.0-
user=> (println (read-line))
this is my input
this is my input

To use it in an if statement, you'd probably use let:

(let [yayinput (read-line)]
  (if (= yayinput "1234")
    (println "Correct")
    (println "Wrong")))

Hope that's enough to get you started, because that's about the limit of my Clojure knowledge!

Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150
dbr
  • 165,801
  • 69
  • 278
  • 343
32

Remember also that you have access to all of Java ...

OK so perhaps I should present some examples ... my clojure skills are not good so these examples may need a bit of tweaking.

The System.console() way:

(let [console (. System console)
     pwd (.readPassword console "tell me your password: ")]
   (println "your password is " pwd))

The BufferedReader way:

(print "give me a line: ")
(let [reader (java.io.BufferedReader. *in*)
     ln (.readLine reader)]
   (println "your line is " ln))

My point is that one can leverage knowledge of Java, and Java itself, in Clojure. It's one of its principal, advertised strengths.

Wonder what would my score have been if the question were about user input from a GUI!

By the way, you could use JOptionPane to put up a little GUI to get user input ...

Allen
  • 2,228
  • 18
  • 22
  • 4
    So being able to call System.console()'s getPassword()/readLine() is what? Seriously, the question is about getting user input in Clojure ... a JVM language. – Allen Jul 13 '09 at 22:04
  • 9
    Uh, this is where Clojure *has* to meet Java. If you are going to program Clojure like this, you might as well use nothing. – Allen Oct 27 '09 at 03:40
  • 13
    geez, no love for java interop here. This is a good answer and the discouragement from the peanut gallery is unnecessary. How do you think read-lines is implemented? It's done with BurfferedReaders of course. – Robert McIntyre Jan 20 '11 at 09:10
  • `System/console`? – jiyinyiyong Oct 22 '17 at 16:46
  • 1
    Does Clojure automatically close the `BufferedReader`? – m0skit0 Jul 04 '18 at 14:11
  • @m0skit0 Looking at the source of `read-line`, there seems to be no need to close `BufferedReader`. – mzuther Dec 04 '19 at 09:02
  • @mzuther So does it close it? – m0skit0 Dec 04 '19 at 10:52
  • @m0skit0 Sorry if my comment wasn't very clear. `BufferedReader` is not explicitly closed (see [here](https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core.clj#L3796)). – mzuther Dec 04 '19 at 14:13
  • 1
    @mzuther Ok I see, then the answer should probably close the reader :) – m0skit0 Dec 06 '19 at 02:25
  • Although this is an old issue, it is not perfect, as the returned `password` is a `byte[]`. Printing it displays the ugly toString of Java array. See https://gist.github.com/ampersanda/4647d16a5d335dce8dcd49ff47fd851e for a complete solution for passwords. – Kineolyan Nov 07 '21 at 08:13
2

read-line is used to get user input and use let to bind it to some variable.

For example : if you want to read user ID and password from user and display it, you could use the following piece of code

(defn print-message [pid pw] 
(println "PID : " pid)
 (println "PW : " pw))

(defn inp[]
(println "Enter your PID and password") 
(let[pid (read-line) pw (read-line)] 
(print-message pid pw) ))
Vj l
  • 71
  • 8
  • 7
    You shouldn’t use `read-line` to read a password. Use a variant of `(.readPassword (System/console) "password: ")` instead in order to mask the password on the console. – bfontaine Apr 13 '17 at 10:23