forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh04Q21.java
More file actions
executable file
·44 lines (42 loc) · 1.45 KB
/
Ch04Q21.java
File metadata and controls
executable file
·44 lines (42 loc) · 1.45 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
/* */ package ch04;
/* */
/* */ import java.util.Scanner;
/* */
/* */ public class Ch04Q21 {
/* */ public static void main(String[] args) {
/* 7 */ int INVALID_LENGTH = 11;
/* 8 */ Scanner scanner = new Scanner(System.in);
/* 9 */ System.out.print("Enter a SSN: ");
/* 10 */ String ssn = scanner.nextLine();
/* */
/* 12 */ if (ssn.length() != 11) {
/* 13 */ System.out.println("Invalid length.");
/* 14 */ System.exit(1);
/* */ }
/* 16 */ else if (!isNumberValid(ssn)) {
/* 17 */ System.out.println("Invalid number.");
/* 18 */ System.exit(2);
/* */ }
/* 20 */ else if (ssn.charAt(3) != '-' || ssn.charAt(6) != '-') {
/* 21 */ System.out.println("Invalid separator.");
/* 22 */ System.exit(3);
/* */ }
/* 24 */ System.out.println(ssn + " is a valid social security number.");
/* */ }
/* */
/* */ public static boolean isNumberValid(String ssn) {
/* 28 */ boolean b = true;
/* 29 */ int[] pos = { 0, 1, 2, 4, 5, 7, 8, 9, 10 };
/* 30 */ for (int i = 0; i < pos.length; i++) {
/* 31 */ if (!Character.isDigit(ssn.charAt(pos[i]))) {
/* 32 */ b = false;
/* */ break;
/* */ }
/* */ }
/* 36 */ return b;
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch04/Ch04Q21.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/