Clojure (read-line) does not wait for input

I am writing a text game in Clojure. I want the player to type lines on the console, and the game will respond in turn.

Research showed me that (read-line)this is the way I need to get text strings from standard input in Clojure, but it does not work for me.

I am in a new Leiningen project, and I have added a sentence :mainto project.cljindicating a single source file:

(ns textgame.core)

(defn -main [& args]
  (println "Entering -main")
;  (flush)                      ;makes no difference if flush are commented out
  (let [input (read-line)]
    (println "ECHO:" input))
;  (flush)
  (println "Exiting -main"))

using lein rungives:

Entering -main
ECHO: nil
Exiting -main

In other words, there is no way to enter text on the console (read-line)for reading.

How do I get Clojure to wait for input of characters and a new line and return the corresponding line?

( GNOME Terminal 2.32.1 Linux Mint 11, Leiningen 1.6.1.1 Java 1.6.0_26 Java HotSpot (TM) 64- VM, Clojure 1.2.1.)

: lein repl, (println (read-line)), , -main lein run.

+5
4

, jar, .

lein uberjar
java -jar project-standalone.jar

, . , repl,

+1

lein. . http://groups.google.com/group/leiningen/browse_thread/thread/a07a7f10edb77c9b https://github.com/technomancy/leiningen

: stdin .

A: , Leiningen , . , , read-line, , , , - . , JVM Leiningen, .

+6

Wrap your read requests with a macro with read-line support, which is now in ns swank.core [as I consider swank-clojure 1.4+]:

(use 'swank.core)
(with-read-line-support 
  (println "a line from Emacs:" (read-line)))

Thanks to Tavis Judd for the fix.

+1
source

Not sure about the aspects of the problem, but it is definitely not possible to make stdin work in emacs. However, if you want to get text from the user, you can easily do this using JOptionPane, like this code, from my little tic-tac-toe program:

(defn get-input []
  (let [input (JOptionPane/showInputDialog "Enter your next move (row/column)")]
    (map #(Integer/valueOf %) (.split input "/"))))
0
source

All Articles