- Use nice indentation using 4 spaces. This improves the readability a lot. See the official guidelines on indentation here: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#262here.
Basically every time you open a new curly brace you should start a new indentation depth. (See code sample below)
2. Use braces around all kinds of blocks, e.g. here even for one-lined if/else blocks. Also, for Java it is the official convention to place the braces like this:
if (line.equals("")) {
truefalse = false;
} else {
System.out.println(line);
}
You can use a trick to assign the value of the scanner input to a variable inside the while condition and then use the String's
isEmpty()function to check if the input was empty. This way you can spare the boolean variable indicating whether something was read:while (!(line = input.nextLine()).isEmpty())Use try() to create instances of Types that implement
java.lang.AutoCloseableto make sure they are automatically closed when the block ends (Note this only works with Java 7 or newer):try (Scanner input = new Scanner(System.in))
Use braces around all kinds of blocks, e.g. here even for one-lined
if/elseblocks. Also, for Java it is the official convention to place the braces like this:if (line.equals("")) { truefalse = false; } else { System.out.println(line); }You can use a trick to assign the value of the scanner input to a variable inside the while condition and then use the String's
isEmpty()function to check if the input was empty. This way you can spare thebooleanvariable indicating whether something was read:while (!(line = input.nextLine()).isEmpty())Use
try()to create instances ofTypesthat implementjava.lang.AutoCloseableto make sure they are automatically closed when the block ends (Note this only works with Java 7 or newer):try (Scanner input = new Scanner(System.in))
See here for reference: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.htmlhere for reference.
public class Morse {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
String line;
while (!(line = input.nextLine()).isEmpty()) {
System.out.println(line);
}
}
}
}