Skip to main content
added 8 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  1. 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);
      }
  1. 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())

  2. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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))

  1. 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);
     }
    
  2. 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())
    
  3. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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);
            }
        }
    }
 
}
  1. 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#262

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);
      }
  1. 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())

  2. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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.html

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);
            }
        }
    }
 
}
  1. Use nice indentation using 4 spaces. This improves the readability a lot. See the official guidelines on indentation here.

Basically every time you open a new curly brace you should start a new indentation depth. (See code sample below)

  1. 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);
     }
    
  2. 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())
    
  3. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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.

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);
            }
        }
    }
}
Add indentation review
Source Link
Nihathrael
  • 651
  • 4
  • 12
  1. 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);
      }
    
  2. 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())

  3. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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 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#262

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);
      }
  1. 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())

  2. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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))

  1. 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);
      }
    
  2. 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())

  3. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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))

  1. 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#262

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);
      }
  1. 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())

  2. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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))

Source Link
Nihathrael
  • 651
  • 4
  • 12

There are a number of ways your code can be improved:

  1. 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);
      }
    
  2. 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())

  3. Use try() to create instances of Types that implement java.lang.AutoCloseable to 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.html

Putting that all together:

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);
            }
        }
    }

}