4 Replies - 3313 Views - Last Post: 27 November 2008 - 09:04 PM Rate Topic: -----

#1 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Out of bounds exception error

Post icon  Posted 27 November 2008 - 08:05 PM

Hello there everyone,

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;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Out of bounds exception error

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Out of bounds exception error

Posted 27 November 2008 - 08:19 PM

Opps.. wrong thread! Sorry! :)

This post has been edited by n8wxs: 27 November 2008 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,101
  • Joined: 26-September 08

Re: Out of bounds exception error

Posted 27 November 2008 - 08:22 PM

Ok Mark, I don't know where you are getting an out of bounds exception, but let's start here:

		//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));
				
			}
		}


Just to let you know, Character.isLetterOrDigit doesn't clean your array, it returns a boolean value that is either true or false. Pretty much all you are doing to "clean" your array is convert it to lower case. Stay tuned for updates :D

EDIT:

Oh yes, your problem originates from your CompareArray method, your LastCharacter value is getting a one off error. You need to set LastCharacter to the length of the String - 1.

This post has been edited by TriggaMike: 27 November 2008 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Out of bounds exception error

Posted 27 November 2008 - 08:53 PM

Ok super confusing, now it only doesn't work for some words... thanks mike... haha :D :^:
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
				if(Character.isLetterOrDigit(CleanArray[FirstIndex].charAt(SecondIndex)))
				{
					CleanArray[FirstIndex] += Words[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(Words[Lines] + " Is a Palindrome");
			}
			else
			{
				FileOutput.println(Words[Lines] + " Is NOT a Palindrom");
			}
			
			
		}
		
		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))
			{
				System.out.println(Words[Lines] + " Is a Palindrome");
			}
			else
			{
				System.out.println(Words[Lines] + " Is NOT a Palindrom");
			}
		}
		
		FileOutput.close();
	}
	
	/**
	 * 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() - 1;
		
		while(FirstCharacter < Array[ArrayNumber].length())
		{
			if((Array[ArrayNumber].charAt(FirstCharacter)) == (Array[ArrayNumber].charAt(LastCharacter)))
			{
				Compare = true;
				break;
			}

			FirstCharacter++;
			LastCharacter--;
		}
		return Compare;
	}
}

Was This Post Helpful? 0
  • +
  • -

#5 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,101
  • Joined: 26-September 08

Re: Out of bounds exception error

Posted 27 November 2008 - 09:04 PM

nvm, helped on phone

This post has been edited by TriggaMike: 27 November 2008 - 09:27 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1