-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject4.java
More file actions
118 lines (110 loc) · 2.46 KB
/
Project4.java
File metadata and controls
118 lines (110 loc) · 2.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
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
112
113
114
115
116
117
118
/**
Cesar Marin Project 4 TA:Ghazaleh Beigi
The program aks the user for a potential palindrome and then tests
if such string is a plaindrome or not, and what kind it is (word, number or phrase).
*/
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Project4
{
public static void main(String[]args)
{
String line = getInputLine();
while (!isEmptyLine (line)) {
if (isPalindrome (line))
System.out.println ("\"" + line +
"\" is a palindrome and a " + getPalType (line));
else
System.out.println ("\"" + line +
"\" is not a palindrome");
line = getInputLine();
}
System.out.println ("End of program");
}
/**
Gets input from the user (potential palindrome)
@param
@return string
*/
public static String getInputLine()
{
String palindrome = JOptionPane.showInputDialog ("Please enter a potential palindrome");
return palindrome;
}
/**
Tests if the input line is empty
@param string str, the input from user
@return boolean true if it is empty, false if its not
*/
public static boolean isEmptyLine(String str)
{
boolean empty= false;
if( str.length()== 0)
empty = true;
return empty;
}
/**
Test to see which kind of palindrome the input is
@param string str, the input from the user
@return string either "word", "number", or "phrase"
*/
public static String getPalType(String str)
{
String palType = "";
int counter = 0;
while(str.length()>counter)
{
char ch = str.charAt(counter);
if(ch>= 48 && ch<=57)
palType= "number";
else
{
if((ch <= 'Z' && ch >= 'A') ||(ch>= 'a' && ch<= 'z'))
palType = "word";
else
{
palType = "phrase";
counter = str.length() +1;
}
}
counter++;
}
return palType;
}
/**
Test to see if the input is a palindrome or not
@param string str, input from user
@return boolean, true if it is a palindrome, false if not
*/
public static boolean isPalindrome(String str)
{
int left = 0;
int right = str.length()-1;
boolean okay = true;
while( okay && left<right)
{
char ch1 = str.charAt(left);
if( !(Character.isDigit(ch1) || Character.isLetter(ch1)))
left++;
else
{
char ch2 = str.charAt(right);
if(!(Character.isDigit(ch2) || Character.isLetter(ch2)))
right--;
else
{
ch1 = Character.toUpperCase(ch1);
ch2 = Character.toUpperCase(ch2);
if( ch1 == ch2)
{
left ++;
right--;
}
else
okay = false;
}
}
}
return okay;
}
}