-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneword.java
More file actions
111 lines (93 loc) · 3.14 KB
/
Phoneword.java
File metadata and controls
111 lines (93 loc) · 3.14 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Sukhamrit Singh
// Phoneword
/*
This program prompts the user for a “phone word,”
an alphabetic mnemonic for a phone number.
Then, print out the phone number corresponding
to that sequence.
*/
// Imports library for user input
import java.util.Scanner;
public class Phoneword {
public static void main(String[] args) {
// Loop to keep getting user input
while (true) {
// Made a scanner
Scanner input = new Scanner(System.in);
// Displays Text
System.out.print("Enter Word here: ");
// The users input
String userWord = input.nextLine();
// Turns the input into all caps
userWord = userWord.toUpperCase();
// Replaces all special characters
userWord = userWord.replaceAll("[^a-zA-Z0-9]", "");
// If statement for string length
if (userWord.length() < 7) {
// Displays message
System.out.println("Your phone word is not long enough " +
"for a phone number");
System.out.println("");
continue;
}
// For loop to turn string to characters
for (int i = 0; i < 7; i++) {
char c = userWord.charAt(i);
// Switch statement for turning characters to numbers
switch (c) {
case 'A':
case 'B':
case 'C':
System.out.print("2");
break;
case 'D':
case 'E':
case 'F':
System.out.print("3");
break;
case 'G':
case 'H':
case 'I':
System.out.print("4");
break;
case 'J':
case 'K':
case 'L':
System.out.print("5");
break;
case 'M':
case 'N':
case 'O':
System.out.print("6");
break;
case 'P':
case 'Q':
case 'R':
case 'S':
System.out.print("7");
break;
case 'T':
case 'U':
case 'V':
System.out.print("8");
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
System.out.print("9");
break;
default:
System.out.print(c);
}
// If statement to print "-"
if (i == 2) {
System.out.print("-");
}
}
// Displays two empty lines
System.out.println("");
System.out.println("");
}
}
}