For my assignment I have to write and test a Java program to read in multiple lines of input until an empty line is read. After each line is read, I have to determine if the line contains a palindrome and, if it does contain a palindrome, I must print which type of palindrome it is (word, phrase, or number). In order to do the palindrome part I have to use a pseudo code.
The pseudo code is:
Note: in the following, the symbol represents assignment
left 0
right position of last character in string
okay true
while okay and left < right
ch1 character in the string at position (left)
if ch1 is not a digit or letter
increment left
else
ch2 character in the string at position (right)
if ch2 is not a digit or letter
decrement right
else
convert both ch1 and ch2 to upper case
if ch1 = ch2
increment left
decrement right
else
okay false
endif
endif
endif
end while
return okay
What I have so far is:
import java.util.Scanner;
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");
}
public static String getInputLine()
{
System.out.println("Enter a line of input: ");
Scanner in = new Scanner(System.in);
String line = in.next();
System.out.println(line);
return line;
}
public static boolean isEmptyLine (String str)
{
boolean isEmptyLine;
if( str == null)
isEmptyLine = true;
else
isEmptyLine = false;
return true;
}
public static boolean isPalindrome (String str)
{
Scanner word = new Scanner(System.in);
String isPalindrome = word.next();
int strLength = isPalindrome.length();
while(true && 0 < isPalindrome.charAt(isPalindrome.length() -1))
{
if(Character.isDigit(strLength) || Character.isLetter(0))
{
I have not finished it but I need help with understanding how to use the pseudo code. I don't quite understand the first if statement part. If anyone has time to explain the code I would greatly appreciate it.
The best way I've found to understand pseudo-code like this is just to go through it using a piece of paper before you start to write any code. Write out something like "abcdcba"
Use your left and right index fingers to track the variables left and right. You'll start with your left finger on the left-most character, and your right at the last character. Now just step through the instructions.
The basic idea is to compare the character at left with the one at right. If it's the same, then move left one position to the right, and move right one position to the left. The repeat.
You'll skip over any non-alphanumeric characters along the way. Characters are also converted to upper-case to avoid any case sensitivity.
If at any time the character at left does not match the character at right, then we don't have a palindrome. If left and right meet, or pass each other, and everything has been a match up to that point, then we do have one.
Since you asked about the first if statement, know that Java's Character class provides
public static boolean isLetterOrDigit(char ch)
you can use:
if (! Character.isLetterOrDigit(ch1)) { ...
As a side note, your isEmptyLine() method always returns true. The entire method should be re-written as:
public static boolean isEmptyLine(String str) {
return (str == null);
}
And for that matter, the method could be deleted completely, and just re-write your while loop as:
while(line != null) {