-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerNumber.java
More file actions
57 lines (47 loc) · 1.46 KB
/
CustomerNumber.java
File metadata and controls
57 lines (47 loc) · 1.46 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
/**
* This program test a customer ID to
* verify that it is in the proper format.
*/
import java.util.*;
public class CustomerNumber
{
public static void main(String[] args)
{
String input;
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a customer ID in the form LLLNNNN");;
System.out.print("(LLL = letters and NNNN = numbers): ");
input = keyboard.nextLine();
if (isValid(input))
System.out.println("That's a valid customer ID.");
else
{
System.out.println("That is not the proper format of " +
"a customer ID.");
System.out.println("Here is an example: ABC1234");
}
}
private static boolean isValid(String custID)
{
boolean goodSoFar = true; //Flag
int i= 0;
//Test the length
if(custID.length() != 7)
goodSoFar = false;
//Test the first three characters for letters.
while(goodSoFar && i < 3)
{
if (!Character.isLetter(custID.charAt(i)))
goodSoFar = false;
i++;
}
//Test the last four characters for digits.
while (goodSoFar && i < 7)
{
if (!Character.isDigit(custID.charAt(i)))
goodSoFar = false;
i++;
}
return goodSoFar;
}
}