Summary: In this tutorial, you will learn to take input and give output to users in Java with the help of the examples.

Read User Input in Java

In Java, you can use the Scanner class to read input from the user. Here is an example of how to use the Scanner class to read a string from the user:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String input = scanner.nextLine();
    System.out.println("You entered: " + input);
  }
}

This will print a prompt asking the user to enter a string, and then it will read the user’s input and store it in the input variable.

You can also use the Scanner class to read other types of input, such as integers and doubles. For example, to read an integer, you can use the nextInt() method:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int input = scanner.nextInt();
    System.out.println("You entered: " + input);
  }
}

You can use the nextDouble() method to read a double, and the nextBoolean() method to read a boolean value.

It’s important to note that the Scanner class can throw a InputMismatchException if the user enters a value that is not of the expected type. (You will read about Exception in later posts)

For example, if you use the nextInt() method to read an integer and the user enters a string, the Scanner will throw an InputMismatchException. You can use a try-catch block to handle this exception and prompt the user to enter a valid value.

Here is an example of how to use a try-catch block to handle the InputMismatchException:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int input = 0;
    boolean valid = false;
    while (!valid) {
      try {
        System.out.print("Enter an integer: ");
        input = scanner.nextInt();
        valid = true;
      } catch (InputMismatchException e) {
        System.out.println("Invalid input. Please enter an integer.");
        scanner.nextLine();  // consume the invalid input
      }
    }
    System.out.println("You entered: " + input);
  }
}

This will keep prompting the user to enter an integer until they provide a valid input.

Output to Console in Java

In Java, you can use the System.out.println() method to output a line of text to the console. Here is an example of how to use System.out.println() to output a string to the console:

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

This will print the string “Hello, World!” to the console.

You can also use the System.out.print() method to output text to the console without adding a newline at the end. For example:

public class Main {
  public static void main(String[] args) {
    System.out.print("Hello, ");
    System.out.println("World!");
  }
}

This will print “Hello, World!” to the console, with the words “Hello, ” and “World!” on same line.

You can also use the System.out.printf() method to format the output using a format string. For example:

public class Main {
  public static void main(String[] args) {
    int x = 10;
    double y = 5.5;
    System.out.printf("x = %d, y = %.1f", x, y);
  }
}

This will print “x = 10, y = 5.5” to the console. The %d in the format string specifies that an integer value should be printed, and the %.1f specifies that a floating-point value should be printed with one digit after the decimal point.

You can use a variety of format specifiers in the format string to control the output, such as %s for strings, %c for characters, and %x for hexadecimal values.

Conclusion

To summarize, in Java you can use the Scanner class to read input from the user, and the System.out.println(), System.out.print(), and System.out.printf() methods to output text to the console.

The System.out.println() method outputs a line of text to the console, while the System.out.print() method outputs text to the console without adding a newline at the end.

The System.out.printf() method allows you to format the output using a format string and a variety of format specifiers.


Leave a Reply