0
How to write a program that accepts a date in dd/mm/yyyy format and separates the day, month and year as seperate integers.
Please write the program as simple as possible as I am new in this.
1 ответ
+ 3
You can change the token delimiter of the scanner to "/":
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in).useDelimiter("/");
        System.out.println("Please enter a date in dd/mm/yyyy format: ");
        String dd = sc.next();
        String mm = sc.next();
        String yyyy = sc.next();
        System.out.println(dd);
        System.out.println(mm);
        System.out.println(yyyy);
    }
}
Alternatively, you can use the split function:
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a date in dd/mm/yyyy format: ");
        String d = sc.nextLine();
        for (String elem: d.split("/")) {
            System.out.println(elem);
        }
    }
}



