Java reminders

Reading from the keyboard (1/2)

The class Scanner allows to easily read from the keyboard.

Scanner scan = new Scanner(System.in);

while(scan.hasNextLine()){
	String currentString = scan.nextLine();
	...
}

Reads lines from the keyboard until the user types Control+D.

Warning scan.hasNext() et scan.next() simply read a "word" from the keyboard and not a full line.

Reading from the keyboard (2/2)

To read Integers:

Scanner scan = new Scanner(System.in);

while(scan.hasNextInt()){
	int currentInt = scan.nextInt();
	...
}

To read Longs:

Scanner scan = new Scanner(System.in);

while(scan.hasNextLong()){
	long currentLong = scan.nextLong();
	...
}

Running a Java program in a terminal (1/2)

In a terminal, test that java launches the correct version of java. The version should be 13.

% java -version

If it is not the case, you need to edit the file ~/.bashrc with:

% gedit ~/.bashrc

and add the line:

alias java="/usr/local/apps/java13/bin/java"

and open a new terminal.

Running a Java program in a terminal (2/2)

To run a jar file Toto.jar, you type in the directory of the jar

% java -jar Toto.jar

To run your own code, you need to be in the bin directory of your workspace.

If you want to run the main of the class Tata in the package fr.upem.net, you need to type (at the root of bin directory):

% java fr.upem.net.Tata 

or:

% java fr/upem/net/Tata