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.

27th Sep 2016, 9:53 AM
Harsh kumar
Harsh kumar - avatar
1 Réponse
+ 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); } } }
27th Sep 2016, 10:14 AM
Zen
Zen - avatar