/** * I/O Cheat Sheet * Version 0.1 * Author: Musard Balliu * It describes how to perform I/O using standard Java * or the course library dit948 */ **** I/O using standard Java commands****** Reading user-provided input from the console in Java ---------------------------------------------------- Step 1: Create an Scanner object in the "main" method Scanner input = new Scanner(System.in); Step 2: Use the following expressions to input a value of a give type input.nextInt() // reads an integer value (e.g. 1, -5, 321) from the console input.nextDouble() // reads a real value (e.g. 3.2, 1.32432, 77) from the console input.nextBoolean() // reads a boolean value (e.g. true, false) from the console input.next() // reads a string (up to the first white space) from the console. If the input is "Hello World" is will only read "Hello". input.nextLine() // reads the entire string (including white spaces) from the console. If the input is "Hello World" is will read "Hello World" input.nextFloat() // reads a float value input.nextLong() // reads a long value input.nextByte() // reads a byte value input.nextShort() // reads a short value Writing output to the console in Java -------------------------------------- Step 1: Type the following commands System.out.println(output) // prints to the console whatever value is in "output" and move to the next line System.out.print(output) // prints to the console whatever value is in "output" without moving to the next line. **** I/O using library dit948****** Reading user-provided input from the console in Java ---------------------------------------------------- Step 1: Create a package dit948 in your Eclipse project and copy the file SimpleIO.java. Step 2: Copy/paste the following line in the Java file you have created to implement your task. (see Template.java as an example) import static dit948.SimpleIO.*; Step 3: Use the following expressions to input a value of a give type readInt() // reads an integer value (e.g. 1, -5, 321) from the console readDouble() // reads a real value (e.g. 3.2, 1.32432, 77) from the console readBoolean() // reads a boolean value (e.g. true, false) from the console readString() // reads a string (up to the first white space) from the console. If the input is "Hello World" is will only read "Hello". readLine() // reads the entire string (including white spaces) from the console. If the input is "Hello World" is will read "Hello World" readChar() // reads a character (e.g. 'a', '7', '+') from the console readFloat() // reads a float value readLong() // reads a long value readByte() // reads a byte value readShort() // reads a short value Writing output to the console in Java -------------------------------------- Step 1: the same as above Step 2: the same as above Step 3: Type the following commands println(output) // prints to the console whatever value is in "output" and move to the next line print(output) // prints to the console whatever value is in "output" without moving to the next line. Generating random input values in Java -------------------------------------- Step 1 and 2: almost the same as above. Just use Random.java instead of SimpleIO.java Step 3: Type the following commands randomInt() // generates a random integer value between -2^31 and 2^31 - 1 randomInt(int n) // generates a random integer in the range (0, 1, ..., n-1) Example: random(100) generates an integer between 0 and 99 randomBoolean() // generates a random boolean value, namely either true or false randomChar() // generates a random character randomDouble() // generates a random real number