HI, I have to make a Java progrma to find Palindromes that reads from a .txt and
ID: 3753057 • Letter: H
Question
HI,
I have to make a Java progrma to find Palindromes that reads from a .txt and uses a isPalindrome private method. This started as a simple find palindrome file, then the isPalindrome was added affter the fact. Here is what i have so far.
public class FileReading {
// gotta do this 'throws IOException' thing, to be explained later
public static void main(String[] args) throws IOException {
String str, reverse = "";
Scanner fileIn = new Scanner(new File("simpleStart.txt"));
while (fileIn.hasNext()) {
str = fileIn.nextLine();
int length = str.length();
for (int i = str.length() - 1; i >= 0; i--) {
reverse = reverse + str.charAt(i);
char p = str.charAt(i); //not sure about this part
if (isPalindrome(p)) { //Thinking this is how to get in the private method
}
System.out.println("Entered string is a palindrome.");
}
reverse = "";
}
}
private static boolean isPalindrome (char p){
if (str.equals(reverse)) { //here is where i am stuck, trying to move the comparison into the private method, unless i'm way off base and down here is used for another portion of the Palindrome.
return true;
} else {
return false;
}
}
}
Explanation / Answer
import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileReading { // gotta do this 'throws IOException' thing, to be explained later public static void main(String[] args) throws IOException { Scanner fileIn = new Scanner(new File("simpleStart.txt")); String word; while (fileIn.hasNext()) { word = fileIn.next(); if(isPalindrome(word)) { System.out.println(word + " is a palindrome"); } else { System.out.println(word + " is not a palindrome"); } } } private static boolean isPalindrome(String s) { if(s.length()Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.