83.2k views
2 votes
Please help with coding from a beginner's computer sci. class (Language=Java)

Assignment Details=

Create a class called Palindromes, which will determine if a user’s entry is a palindrome. A
palindrome is a word or phrase that reads the same backward or forwards. See the examples.
A Santa at NASA My gym taco cat kayak NOW I WON race car
Your program will ignore spaces and ignore cases when determining if a string is a palindrome.
Use the String method replace to remove the spaces. If it is a palindrome, the output
PALINDROME, or else output NOT PALINDROME.
Additional requirements:
 Add an infinite loop with a sentinel-trigger break test (allowing them to input multiple
attempts). You may decide on its command word or exit condition.
 As below, make sure a spacer line exists between each run of the program
Here are two sample runs of the program.
Enter a word or phrase: Noon
PALINDROME
Enter a word or phrase: bookkeeper
NOT PALINDROME

User Aadarshsg
by
5.0k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

import java.util.Scanner;

public class Palindromes {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String word;

while (true) {

System.out.print("Enter a word or phrase: ");

word = input.nextLine();

if (word.equals("exit")) {

break;

}

String modifiedWord = word.replace(" ", "").toLowerCase();

if (modifiedWord.equals(new StringBuilder(modifiedWord).reverse().toString())) {

System.out.println("PALINDROME");

} else {

System.out.println("NOT PALINDROME");

}

System.out.println();

}

input.close();

}

}

User Frooyo
by
5.7k points