33

How do I request user input from a running script in Julia? In MATLAB, I would do:

result = input(prompt)

Thanks

ejang
  • 3,982
  • 8
  • 44
  • 70

5 Answers5

34

The easiest thing to do is readline(stdin). Is that what you're looking for?

scls
  • 16,591
  • 10
  • 44
  • 55
John Myles White
  • 2,889
  • 20
  • 14
  • 5
    We could stand to have a more sophisticated readline-library-like system for this, but for now this will do the trick. Keno's pure Julia reimplementation of our repl will give provide a nice framework for doing interactive stuff like this. – StefanKarpinski Jul 05 '13 at 16:34
  • 2
    On julia 0.7 and later (possibly 0.6) this is now `stdin`. – niczky12 Sep 04 '18 at 11:15
19

I like to define it like this:

julia> @doc """
           input(prompt::AbstractString="")::String

       Read a string from STDIN. The trailing newline is stripped.

       The prompt string, if given, is printed to standard output without a
       trailing newline before reading input.
       """ ->
       function input(prompt::AbstractString="")::String
           print(prompt)
           return chomp(readline())
       end
input (generic function with 2 methods)

julia> x = parse(Int, input());
42

julia> typeof(ans)
Int64

julia> name = input("What is your name? ");
What is your name? Ismael

julia> typeof(name)
String

help?> input
search: input

  input(prompt::AbstractString="")::String

  Read a string from STDIN. The trailing newline is stripped.

  The prompt string, if given, is printed to standard output without a trailing newline before reading input.

julia>
HarmonicaMuse
  • 7,633
  • 37
  • 52
2

A function that checks that the answer provided matches the expected Type:

Function definition:

function getUserInput(T=String,msg="")
  print("$msg ")
  if T == String
      return readline()
  else
    try
      return parse(T,readline())
    catch
     println("Sorry, I could not interpret your answer. Please try again")
     getUserInput(T,msg)
    end
  end
end

Function call (usage):

sentence = getUserInput(String,"Write a sentence:");
n        = getUserInput(Int64,"Write a number:");
Antonello
  • 6,092
  • 3
  • 31
  • 56
2

Now in Julia 1.6.1, it's as simple as typing:

num = readline()

Yea! without any arguments since the default value for the IO positional argument of the readline() function is "stdin". So in the above example Julia will read the input from the user and store it in the variable "num".

winterr_dog
  • 49
  • 1
  • 6
  • 1
    This should now be the accepted answer, please upvote. This seems to be somewhat a common problem with Julia, where many stackoverflow highly voted/accepted answers are out of date. By the way, note this functionality hasn't worked in VS Code for over 4 years, which I believe is the "standard" editor for Julia. See https://github.com/julia-vscode/julia-vscode/issues/785 – GeoffDS Aug 11 '23 at 16:46
-4

First I ran Pkg.add("Dates") then

using Dates

println()
print("enter year  "); year = int(readline(STDIN))
print("enter month "); month = int(readline(STDIN))
print("enter day   "); day = int(readline(STDIN))

date = Date(year, month, day)
println(date)
Barry Andersen
  • 457
  • 2
  • 7
  • 9