-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02.b.StringTokenizer.java
More file actions
58 lines (49 loc) · 1.74 KB
/
02.b.StringTokenizer.java
File metadata and controls
58 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
2.b. Write a Java class called Customer to store their name and date_of_birth. The
date_of_birth format should be dd/mm/yyyy. Write methods to read customer data as
<name, dd/mm/yyyy> and display as <name, dd, mm, yyyy> using StringTokenizer
class considering the delimiter character as "/".
*/
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringJoiner;
import java.util.StringTokenizer;
class Customer {
String name;
String dob;
void accept() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Name and DoB");
String input = scanner.nextLine();
StringTokenizer stringTokenizer = new StringTokenizer(input, ",");
try {
name = stringTokenizer.nextToken().trim();
dob = stringTokenizer.nextToken().trim();
} catch (NoSuchElementException e) {
System.out.println("Invalid Format");
System.exit(0);
}
}
void display() {
StringTokenizer stringTokenizer = new StringTokenizer(dob, "/");
try {
String dd = stringTokenizer.nextToken().trim();
String mm = stringTokenizer.nextToken().trim();
String yy = stringTokenizer.nextToken().trim();
StringJoiner stringJoiner = new StringJoiner(", ");
stringJoiner.add(name).add(dd).add(mm).add(yy);
System.out.println(stringJoiner.toString());
} catch (NoSuchElementException e) {
System.out.println("Invalid Format");
System.exit(0);
}
}
}
class Main {
static Customer customer;
public static void main(String[] args) {
customer = new Customer();
customer.accept();
customer.display();
}
}