The infamous palindrome problem, or i heard so.
Hello everybody :) I am a beginner java writer and I have come across a small problem.
Before I talk about it, let me explain what the actual assignment is.
~~~~ASSIGNMENT~~~~
The file /users/abrick/resources/american-english-insane is a very long list of words, one per line. Open the file and identify the three palindromes more than seven letters long (for the record, they are the names of a Dravidian language, a trademarked farm implement, and a mixture of herbs).
So to break it down in steps,
1. Open the file
2. Test the file for palindromes
3. Display the palindromes.
My code:
Code Java:
/*Programs purpose: Tests files for palindromes that are more than 7 letters long. */
//Step 1: Open file
//Step 2: Identify whether or not if the word is a palindrome
//Step 3: Display palindrome
import java.util.Scanner; // Needed for the Scanner Class
import java.io.*; // Needed for File and IOException
public class assignment8
{
public static void main(String []args) throws IOException
{
// Created a Scanner object for keyboard input.
Scanner input = new Scanner(System.in);
// Prompts user for file name
System.out.print("Enter the name of the file to test: ");
String dictionary = input.nextLine();
// Opens the file
File file = new File(dictionary);
Scanner inputFile = new Scanner(file);
// If there are palindromes, they will be displayed
if(palindromeCheck(dictionary))
{
System.out.println(dictionary+ "is a palindrome");
}
}
// Checks for palindromes
public static boolean palindromeCheck(file dictionary)
{
int low = 0;
int high = dictionary.length() -1;
while (low < high)
{
if (dictionary.charAt(low) != dictionary.charAt(high))
{
return false;
}
low++;
high--;
}
return true;
}
}
So what's the problem with my code?
well.....
1. It compiles
2. It runs fine
3. But it doesn't print out the palindromes!
Anybody have any ideas on whats wrong with my code? I need to display the palindromes :\
Re: The infamous palindrome problem, or i heard so.
bump, nevermind.... it won't work with file types, only with strings