Is there a way to format the input from a user, using the Scanner?

how to take multiple string input in java using scanner
how to get continuous input from user in java
java scanner
java for loop user input
how to take integer input from user in java
how to take string input in java using scanner class
how to take input from user in java
how to read a line in java using scanner

I am trying to build a program that asks a user for their DOB and will throw an error if not in the DD/MM/YYYY format. Does anyone know how to make that happen? I apologize if I didn't do enough research but I couldn't really find anything about formatting the input for a date. Thank you for your help.

You need to process the input after it's read. Regular expressions are very efficient in this task. ^\\d{2}/\\d{2}/\\d{4}$ will check that the user supplied string is in DD/MM/YYYY.

So, you could compare the user input as follows:

    //Do stuff if input is good
    //Do stuff if input is bad

Taking input in Java : Scanner class, Here, nextInt() is a method of the object s of the Scanner class. Let's see an example in which we will input an integer value from the user. import java. This is because you are using Scanner#next method. And if you look at the documentation of that method, it returns the next token read. So, when you read user input using next method, it does not read the newline at the end. Which is then read by the nextLine() inside the while loop.

boolean isDOB(String input) {
        String[] inputs = input.split("/");
            return false;
        int days = Integer.parseInt(inputs[0]);
        int month = Integer.parseInt(inputs[1]);        
        if(days>31 || month>12 || inputs[2].length()!=4) {
            return false;           
        return true;        

3 ways for reading user's input from console in Java, Java code Examples for reading input from the user in the command line ( console) The main purpose of the Scanner class (available since Java 1.5) is input without echoing the characters entered by the user; the format� To validate input the Scanner class provides some hasNextXXX() method that can be use to validate input. For example if we want to check whether the input is a valid integer we can use the hasNextInt() method. In the code snippet below will demonstrate how to validate whether the user provide a positive integer number. The program will repeat

Here is the entire program if this helps you. This also validates the date using DateTimeFormatter. So, if user enters 32/01/2000, it fails.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;

public class DateOfBirth {
    public static void main(String[] args) {
        System.out.println("Please enter your DOB");
        Scanner myScanner = new Scanner(;
        String input =;
        while (!validDate(input)) {
            System.out.println("ERROR: Invalid date. Please enter your DOB");
            input =;

    private static boolean validDate(String input) {
        try {
            DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
            LocalDate.parse(input, inputFormat);
        } catch (DateTimeParseException e) {
            return false;
        return true;

Input and output | Think Java, This chapter will show you how to read input from the keyboard, use that input to calculate a result, and then format that result for output. It's necessary because there might be another class named Scanner in another package. Using an import The following example reads two lines and repeats them back to the user:. To take input from user through Console, we have to import Scanner class from Util Library. import java.util.Scanner; Once we create an object of Scanner Class, we can easily take user input.

Input and Output | Think Java, computation. This chapter shows you how to read input from the keyboard, use that input to calculate a result, and then format that result for output. “utility classes”. Before you can use Scanner , you have to import it like this: Here's a complete example that reads two lines and repeats them back to the user: View on� Example of of reading user input in Java using Scanner. In this section we are explaining the use of Scanner class in Java which can be used to read user input from console. Our sample program asks user to enter name and then age, user input is read using the scanner class. Information entered by user is printed back on the console. There are

Java Input and Output (I/O), Input is any information that is needed by your program to complete its execution. The Scanner class has a method called nextLine that returns a line of text Get a String of characters that is in an integer format, e.g., "123". To let the user know what kind of input the program expects, usually you should call the System.out.print method before you call a Scanner method to get input. Line 10 calls System.out.print to display the message Enter an integer: on the console. That way, the user knows that the program is waiting for input.

Java - Read typesafe input using Scanner class, Learn to read typesafe user inputs from console in any interactive java application i.e. prompt the user until user inputs the value in correct format/ datatype. way of reading input from console using Scanner [Recommended]� We are having special classes to take input from the user. One of the most commonly used class is java.util.Scanner. We can read input from the console using scanner class. Java.util.Scanner class provides lot of methods to take different typed of data from the user as input. Scanner class is final class in java like String class.

  • Possible duplicate of Java: Check the date format of current string is according to required format or not. Getting a String from the user is common enough that you can find info on how to do that as well.
  • Parse the string as a date and catch the parse exception.
  • "and will throw an error if not in the DD/MM/YYYY format" in that case simply accept user data as string, then check if it matches dd/dd/dddd where d is digit. You are use regex for that, or if you want to check if not only format is correct but also content you can try parsing this string into actual date.
  • "I couldn't really find anything about formatting the input for a date" That is because you don't format the input, you parse it. Try searching for that instead.
  • @takendarkk Yes getting a string is easy to do, what I am trying to get is a string formatted a specific way. It doesn't appear that anything in that link addresses that.
  • Where would you put this?
  • Down voter - can you tell me why you have down voted this? Do you see any issue with this solution?