Question: Java coding exercise. Hint: User will enter certain number of random words, you need to read the input as string and evaluate based on the
Java coding exercise.
Hint: User will enter certain number of random words, you need to read the input as string and evaluate based on the following instructions. Write a void method called palindromeCheck that takes NO argument. The method should have functionality to check whether or not the word is a palindrome and print to the screen all of the palindromes, one per line. Also, the last line of the output should have the message: There are x palindromes out of y words provided by user (where x is the number of palindrome words detected, and y is the total number of words entered by user). Hint: for this exercise you will need the following methods on String objects: length() gives the length of a string (that is, the number of characters it contains) and charAt(i) - gives the character at position i. For example: int sizeOfString = "bob".length() //should be 3 char firstChar = "bob".charAt(0) //should be b
Here is a templet that has been provided to code this exercise:
public static void palindromeCheck() {
String someWord = ""; //Stores words read from user input
int count = 0; //keeps track of Palindrome words
int total = 0; //Counts the total number of lines read from the given text file
System.out.println(" Enter some words separated by white space");
Scanner keyboard = new Scanner(System.in);
//hint 1: Using keybord.next() will only return what comes before a space.
//hint 2: Using keybord.nextLine() automatically reads the entire current line.
//for each word user enters
while (keyboard.hasNext()) {
someWord = keyboard.next(); // store each word in a String variable then operate
total++; //increment number of words as you read each one
//Code your logic for how to determine if a word is Palindrome first, then complete # 2
System.out.println(" " + total + " " + someWord);
}
//if encountered ENTER then close scanner stream and terminate
keyboard.close();
//x is a variable for count and y is variable total
//#2. print
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
