Skip to content

Input & Output

Eman Castillo edited this page Dec 20, 2022 · 2 revisions

Input

Unlike Python and C, you need to call the input function from the io module to a variable in a similar fashion to Java's Scanner class.

Once you have a variable you called the function to, you can start using it to read user input. To do that, you would do it similarly to Java by using the read attributes of the reader class that powers input:

Types of reads:

  • readLine: Returns array<char>
  • readInt: Returns int
  • readDec: Returns dec
  • readChar: Returns char
  • readAll: Takes in an array<char> and returns the first entry of that array<char>.

In this small example below, the input function is being called to the variable input and is being used to take in user input.

include "io.nhp"

def main():
    input = io::input()
    favFood = input.readLine()

Output

Like input, you need to call the output function to a variable to be able to have the ability to create output in the console.

Once you have done this, you can create output by using the writeLine attribute of output. Like in C and Python, you can print the value of variables using this.

The example below uses both input and output to take user input and print it.

include "io.nhp"

def main():
	input = io::input()
	out = io::output()
	out.writeLine("What is your favorite highway?")
	highway = input.readLine()
	out.writeLine("Your favorite highway is:")
	out.writeLine(highway)
Clone this wiki locally