Reading user input is a way of interaction between the program and the user. User inputs are necessary in case of testing a piece of code with varying and legitimate inputs.
Reading user inputs from console in Julia can be done through inbuilt I/O methods like :
Using readline() Function
readline() method reads a line of text from the input stream (STDIN) until a ‘\n’ (newline) character is encountered. The input is read as String data.
s = readline()
In the below example, user input is obtained using the
readline() method. We can prompt the user with the help of a print statement prior. It is to be noted that readline method reads the input into a string data type.
Example 2: Reading numerical data types from console
The below example demonstrates how to read inputs as numbers and make use of them in further computations. This is done using the
parse() method, using which we can convert a numeric string(Float or Int) into a numerical value.
Using readlines() Function
readlines() method is used to read N lines of text input from the console. N lines are stored as the entries of a one-dimensional String array. Here lines must be delimited with newline character or by pressing the “ENTER” key. To stop taking Input press Ctrl-D.
lines = readlines()
Example 1: Reading N lines of input from STDIN
In the below example N-lines of input are obtained and stored within a 1-D String Array. We can access the desired line using it’s index.
In the below example we input N lines and display the entries by prefixing with their entry numbers.
- Taking Input from User in R Programming
- Taking input from console in Python
- Julia end Keyword | Marking end of blocks in Julia
- Julia function keyword | Create user-defined functions in Julia
- Julia continue Keyword | Continue iterating to next value of a loop in Julia
- Julia break Keyword | Exiting from a loop in Julia
- Julia local Keyword | Creating a local variable in Julia
- Julia global Keyword | Creating a global variable in Julia
- How to Access All Users in Linux Using Different Commands?
- Getting ceiling value of x in Julia - ceil() Method
- Getting floor value of x in Julia - floor() Method
- Getting the minimum value from a list in Julia - min() Method
- Package Management in Julia
- Visualisation in Julia
- Random Numbers Ecosystem in Julia - The Pseudo Side
- Accessing element at a specific index in Julia - getindex() Method
- Get size of string in Julia - sizeof() Method
- Reverse a string in Julia - reverse() Method
- Fill an array with specific values in Julia | Array fill() method
- Accessing each element of a collection in Julia - foreach() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.