Input and output

For a program to be useful, there must be a way to get data into it (input) and to get the processed result out of the program (output).

Input

Data can be provided as input in different ways:

Hardcoding data into a program has the disadvantage that if the data changes, the hardcoded values in the program must also be changed. This may not be feasible for a very large program with many hardcoded data values.

The option for the user to enter data while the program is running is very useful for interactive programs. One mechanism for doing this in Java is using a dialogue box, and another is using an object called a Scanner.

There are also built-in Java classes that enable data to be read in from a file. If the file is a text file, individual data items are separated by an agreed character such as a comma. If the data is not text-based (for example, sound, or video) it can be read from a binary file that contains bytes of binary data. Java has built-in classes that can read and extract data from such files.

Output

Output can be produced by printing it to a device such as a computer screen or a printer, or by writing it to a file, either as text or as a binary file.

In Java, the easiest way to send output to the default output device (which is usually the computer screen) is to use a statement like this:

System.out.print("Hello world");

There is also a version that prints and then moves to the next line:

System.out.println("Hello world");

In each case, the parameter (the thing in the parentheses) is printed to the screen.

System.out.print and System.out.println can also be used with a variable as the parameter as in the following example:

String myName = "Joan";

System.out.println(myName);

System.out.print("Welcome");

Comparing Java with Python

Input

In Java, an instance of the Scanner class must be created before another built-in method of the Scanner class such as next()can be used to get user input from the terminal window.

Python has a slightly simpler approach in that it provides a built-in method for directly reading input from the user while a program is running.

myName = input('Enter your name')

Both the Java method next() and the Python method input return String objects. If the input needs to be used as a numerical value, then it must then be converted to the desired data type first.

Output

In Python, the method to print text followed by a new line has a shorter name than in Java:

print('Hello world')

And to produce a line of output without moving to the next line you need to indicate the 'end' character, e.g.:

print('Hello world', end = '')

In both Java and Python, the print methods produce character type output. If we use a parameter (what is in the parentheses) that is not of type String then a method called str in Python and toString in Java will automatically be executed on the parameter, to turn it into a String. When a programmer creates a bespoke class in either language it is good practice to rewrite these methods so that a meaningful string description is produced when an attempt is made to print an object of that type. If the programmer does not do this, a default version of the method will be called, but this may not give a very useful description.

Test your understanding

  1. Hardcoding values into a program is good practice. True or false?
  2. Write a fragment of Java code that prints out the message "Welcome to M250" to the standard output device, before moving to the next line and printing "Good luck!". After this second message is printed do not move to the next line.