I'm working on a school project that requires me to read from a file and store those contents in an array, then check to see if the arrays are palindromes are not. The problem i'm having is that they are not all the same size and are ragged arrays and every time i try to read through them i get an Out of bounds error.... here is my code hope anyone has an idea. cheers
import java.util.Scanner;
import java.io.*;
public class Assignment4
{
public static void main(String[] args) throws IOException
{
//Variables
String FileName;
int Counter = 0;
int Index = 0;
//Keyboard instance for input
Scanner Keyboard = new Scanner(System.in);
//Start by asking the user where the file is located
System.out.print("File Name: ");
FileName = Keyboard.nextLine();
//open the file
File FileOpen = new File(FileName);
//Instance to Read data from the file
Scanner FileInput = new Scanner(FileOpen);
//check to see if the file exists and exit if it does not
if(!(FileOpen.exists()))
{
System.out.println("The file does not exist");
System.exit(0);
}
//start a counter to determine how big the size of the file is
while(FileInput.hasNext())
{
//Count the counter to determine how big the file is
FileInput.nextLine();
Counter++;
}
//close the file
FileInput.close();
//Re-Open the file
FileInput = new Scanner(FileOpen);
//create the array to hold the values in the file of the size of the file
String[] Words = new String[Counter];
//read through the file and put it into and array.
//Stop when there is nothing left in the file
while(FileInput.hasNext() && Index < Words.length)
{
//read the data into an array
Words[Index] = FileInput.nextLine();
if(Words[Index] == null)
{
Words[Index] = "";
}
Index++;
}
//transfer the data into a clean array
String[] CleanArray = new String[Counter];
//loop through each line in the array
for(int FirstIndex = 0; FirstIndex < Words.length; FirstIndex++)
{
//loop through each character per array
for(int SecondIndex = 0; SecondIndex < Words[FirstIndex].length(); SecondIndex++)
{
CleanArray[FirstIndex] = Words[FirstIndex].toLowerCase();
//Transfer only Letters and/or characters to the array
Character.isLetterOrDigit(CleanArray[FirstIndex].charAt(SecondIndex));
}
}
//create a new FileOutput for the output text file
PrintWriter FileOutput = new PrintWriter("Output.txt");
//This for loop is to create the file and then send the data to it
for(int Lines = 0; Lines < CleanArray.length; Lines++)
{
// This for loop is to send the array to the method and then return if it is true of not
if(CompareArray(CleanArray, Lines))
{
FileOutput.println(CleanArray[Lines] + " Is a Palindrome");
}
else
{
FileOutput.println(CleanArray[Lines] + " Is NOT a Palindrom");
}
}
}
/**
* this method is used to compare the arrays
* @param Array Pass the reference to the array
* @return a boolean to determine if it is a palindrome or not
*/
public static boolean CompareArray(String[] Array, int ArrayNumber)
{
boolean Compare = false;
int FirstCharacter = 0;
int LastCharacter = Array[ArrayNumber].length();
while(FirstCharacter < Array[ArrayNumber].length())
{
if((Array[ArrayNumber].charAt(FirstCharacter)) == (Array[ArrayNumber].charAt(LastCharacter)))
{
Compare = true;
}
FirstCharacter++;
LastCharacter--;
}
return Compare;
}
}

New Topic/Question
Reply



MultiQuote




|