Preparing for the first programming task submission¶
The first submitted programming task of the course asks you to implement a program that reads several numbers from the user and prints their average value. We will next review some further basic Java features required in the task.
To get the code working as expected, the following line needs to be present in the beginning of the code file:
import java.util.Scanner;
More information regarding imports can be found in the section A more detailed introduction to Java. For this exercise it is sufficient to know that this line is required in order for the Scanner class to be available in your program.
In Java there are several ways to read input from the user, but one of the simplest ones is using the Scanner class. In Java an instance
of a class is created using the new
keyword. When instantiating a class it’s possible to give parameters to
the constructor, much like in C++. In order to use Scanner
to read input from the user, the instance of Scanner
needs to be attached to the standard input System.in
. This can be easily done by giving the standard input
as a parameter to the constructor when creating the scanner instance.
Scanner myScanner = new Scanner(System.in);
To reiterate, the above code creates a new instance of the class Scanner
, attaches it to the standard input
and sets the created instance to the variable myScanner
of type Scanner
. Using this instance we can read
a line of input from the user like so:
String line = myScanner.nextLine();
When the program reaches this line in its execution, the program halts and waits for the user to enter input into the
command line. The user uses the Enter
key to signal the end of the input line, and the entered line is stored in
the variable line
of type String
. In the first exercise it is assumed that the user gives a single line of
input, containing several numbers separated by a space character (' '
). The easiest way to retrieve the individual numbers from
the string is to use the split
method from the String class.
String[] numbers = line.split(" ");
This code snippet takes the variable line
, splits its contents on each space character found, and places the pieces,
which in this case are individual numbers, in an array of String objects called numbers
.
You will most probably need some kind
of a loop in order to process the numbers
array. The basic choice is between while
and for
loops, but the
latter are typically preferred for iterating over some collection of values. Java for
loops have a
very similar syntax with C++.
for (initialization; end condition; step) {
loop body
}
There is also a simpler for
loop variant that iterates over all items in some array or container.
for (iterating variable : iterated container) {
loop body
}
For example the following loop would iterate over the String
array numbers
and print out each String
in the array:
for (String s : numbers) {
System.out.println(s);
}
If you want to iterate over an array in an index-based manner, you first need to know the size of the array.
All Java arrays have a member variable length
that tells the size of the array. Java uses a similar dot-operator
as Python and C++ to refer to member variables, and array value references use a familiar “index inside square brackets”
syntax. The example below shows a loop that prints out all the strings in the array together with their indices. Note how
Java allows you to concatenate values and strings with the +
-operator in a similar manner as, e.g. Python.
for (int i = 0; i < numbers.length; i++) {
System.out.println(i + ": " + numbers[i]); // Prints a line of form "index: parameter".
}
If s
is a String
that represents a number, you may convert it into a double
value with the call
Double.parseDouble(s)
. An example:
String s = "3.14";
double d = Double.parseDouble(s); // The double variable d will hold the value 3.14.
Given these examples, the mean calculation task should be quite straight-forward.