JoeyOpal
04-25-2005, 08:24 PM
Hello,
I am getting an ArrayIndexOutOfBounds error.
I cannot figure out what I am doing wrong or how I can fix it.
Thank you,
Joey
public class PalindromeCheck
{
String charString;
char[] a = new char[80];
int i;
int count = 0;
//boolean charsMatch = true;
boolean result;
//Do I want to add constructors?
//GET INPUT -------------------------------------------------
//PRECONDITION:
public void checkInput()
{
System.out.println("This program will check a string of characters");
System.out.println("and determine whether or not the string is a palindrom
e.");
System.out.println("Input a string of characters followed by a period.");
System.out.println("You must make sure that you include the period at the
end");
System.out.println("of the string or else you will not get correct results
.");
charString = SavitchIn.readLine();
if(charString.length() > 80)//makes sure the user can only enter 80 char m
ax (79 plus the period).
{
System.out.println("You cannot enter more than 79 characters (plus a peri
od)\n");
System.out.println("\nThe program will now exit.\n");
System.out.println("Please run the program again and ");
System.out.println("input the correct string of characters.\n");
System.exit(0); //should this do something different?
}
char a[] = charString.toUpperCase().toCharArray(); //converts a string to
an array of characters
for(int i = 0; i < (a.length); i++)//makes sure the input is either letter
s or blank spaces
{
if(Character.isLetter(a[i]) || Character.isSpaceChar(a[i]))
{
//Maybe check for numbers instead so the "if" statement isn't empty.
//What about symbols such as &, *, and #?
}
else
System.out.println("The input must be a letter or a white space.");
break;
}
System.out.println("\nThe string of characters you entered is: "); //error
checking
for(int i = 0; i < a.length; i++) //displays the array to the screen
{
System.out.print(a[i]);
}
System.out.println("\nIs this the correct string of characters?");
char choice = SavitchIn.readLineNonwhiteChar();
choice = Character.toUpperCase(choice);
if(choice == 'N')
{
System.out.println("\nThe program will now exit.\n");
System.out.println("Please run the program again and ");
System.out.println("input the correct string of characters.\n");
System.exit(0); //should this do something different?
}
else
for(int i = 0; i < a.length; i++)//increments int used once for each arra
y index that is used
{
count++;
}
result = palindrome(a, count);
System.out.println("\n" + (result));
if(result)
{
System.out.println(charString + " IS A PALINDROME.\n");
}
else
System.out.println(charString + " IS NOT A PALINDROME.\n");
}
//POSTCONDITION:
//-------------------------------------------------------------------------
--
//PALINDROME
//PRECONDITION: The array a contains letters and blanks in positions a[0]
through a[used-1].
//Returns true if the string is a palindrome and false otherwise.
//The program will call this method with the array and one other int variab
le.
//The other int variable keeps track of how much of the array is used (part
ially filled array).
public static boolean palindrome(char[] a, int used)
{
int left;
int right = used - 1;
for(left = 0; left < used; left++, right--)
{
if(a[left] != a[right])
{
return false;
}
}
return true;
/*
int i;
for(i = 0; i < (used / 2) ;i++)
{
if(a[i] != a[used - i - 1])
return false;
}
return true;
*/
/*
int left = 0;
int right = a.length - 2;
while(left <= right && charsMatch)
{
if(a[left] == a[right])
{
left++;
right--;
}
else
{
charsMatch = false;
}
}
*/
}
//POSTCONDITION:
//-------------------------------------------------------------------------
-
//DISPLAY ARRAY INFO
//PRECONDITION:
public void displayArrayInfo(int used)
{
System.out.println("\nCount = " + count);
System.out.println("Used = " + used);
System.out.println("\nThe length of the string without the period is " + (
charString.length()-1));
System.out.println("The number of indexes already used is " + count + ".")
; //displays the number of indexes already used
System.out.println("The lowest index used is index a[" + ((a.length-1)-(a.
length-1))); //displays the lowest index
System.out.println("The highest index used is index a[" + (a.length-1) + "
]."); //displays the highest index used
}
//POSTCONDITION:
//-------------------------------------------------------------------------
//EXIT PROGRAM
//PRECONDITION:
public void exitProgram()
{
//boolean repeat = true; //Need a (do while?) loop to continue the program
until the user wants to exit
System.out.println("\nThe program is now exiting.\n");
System.exit(0);
}
//POSTCONDITION:
//-------------------------------------------------------------------------
//MAIN MENU
//PRECONDITION:
public void mainMenu()
{
char choice;
do
{
System.out.println("\n\n\n-----------------------------------------------
-------------");
System.out.println("-----------------------------------------------------
-------");
System.out.println("------This is the main menu. Please make a selection
.------\n");
System.out.println("Please press [1] to input a character string.");
System.out.println("Please press [2] to display array info.");
System.out.println("Please press [3] to exit the program.");
choice = SavitchIn.readLineNonwhiteChar();
switch(choice)
{
case '1': checkInput();
break;
case '2': if(charString == null)
{
System.out.println("\nYou must use option 1 first.\n");
break;
}
displayArrayInfo(count); //gives NullPointerException if this is cho
sen first
break;
case '3': exitProgram();
break;
default: System.out.println("Options 1-3 only please.");
System.out.println("Please enter a selection.\n");
//break;
}
}while(choice != '3');
}
//POSTCONDITION:
//----------------------------------------------------------------------
//MAIN
//PRECONDITION:
public static void main(String[] args)
{
PalindromeCheck checkIt = new PalindromeCheck();
checkIt.mainMenu();
}
//POSTCONDITION:
}
I am getting an ArrayIndexOutOfBounds error.
I cannot figure out what I am doing wrong or how I can fix it.
Thank you,
Joey
public class PalindromeCheck
{
String charString;
char[] a = new char[80];
int i;
int count = 0;
//boolean charsMatch = true;
boolean result;
//Do I want to add constructors?
//GET INPUT -------------------------------------------------
//PRECONDITION:
public void checkInput()
{
System.out.println("This program will check a string of characters");
System.out.println("and determine whether or not the string is a palindrom
e.");
System.out.println("Input a string of characters followed by a period.");
System.out.println("You must make sure that you include the period at the
end");
System.out.println("of the string or else you will not get correct results
.");
charString = SavitchIn.readLine();
if(charString.length() > 80)//makes sure the user can only enter 80 char m
ax (79 plus the period).
{
System.out.println("You cannot enter more than 79 characters (plus a peri
od)\n");
System.out.println("\nThe program will now exit.\n");
System.out.println("Please run the program again and ");
System.out.println("input the correct string of characters.\n");
System.exit(0); //should this do something different?
}
char a[] = charString.toUpperCase().toCharArray(); //converts a string to
an array of characters
for(int i = 0; i < (a.length); i++)//makes sure the input is either letter
s or blank spaces
{
if(Character.isLetter(a[i]) || Character.isSpaceChar(a[i]))
{
//Maybe check for numbers instead so the "if" statement isn't empty.
//What about symbols such as &, *, and #?
}
else
System.out.println("The input must be a letter or a white space.");
break;
}
System.out.println("\nThe string of characters you entered is: "); //error
checking
for(int i = 0; i < a.length; i++) //displays the array to the screen
{
System.out.print(a[i]);
}
System.out.println("\nIs this the correct string of characters?");
char choice = SavitchIn.readLineNonwhiteChar();
choice = Character.toUpperCase(choice);
if(choice == 'N')
{
System.out.println("\nThe program will now exit.\n");
System.out.println("Please run the program again and ");
System.out.println("input the correct string of characters.\n");
System.exit(0); //should this do something different?
}
else
for(int i = 0; i < a.length; i++)//increments int used once for each arra
y index that is used
{
count++;
}
result = palindrome(a, count);
System.out.println("\n" + (result));
if(result)
{
System.out.println(charString + " IS A PALINDROME.\n");
}
else
System.out.println(charString + " IS NOT A PALINDROME.\n");
}
//POSTCONDITION:
//-------------------------------------------------------------------------
--
//PALINDROME
//PRECONDITION: The array a contains letters and blanks in positions a[0]
through a[used-1].
//Returns true if the string is a palindrome and false otherwise.
//The program will call this method with the array and one other int variab
le.
//The other int variable keeps track of how much of the array is used (part
ially filled array).
public static boolean palindrome(char[] a, int used)
{
int left;
int right = used - 1;
for(left = 0; left < used; left++, right--)
{
if(a[left] != a[right])
{
return false;
}
}
return true;
/*
int i;
for(i = 0; i < (used / 2) ;i++)
{
if(a[i] != a[used - i - 1])
return false;
}
return true;
*/
/*
int left = 0;
int right = a.length - 2;
while(left <= right && charsMatch)
{
if(a[left] == a[right])
{
left++;
right--;
}
else
{
charsMatch = false;
}
}
*/
}
//POSTCONDITION:
//-------------------------------------------------------------------------
-
//DISPLAY ARRAY INFO
//PRECONDITION:
public void displayArrayInfo(int used)
{
System.out.println("\nCount = " + count);
System.out.println("Used = " + used);
System.out.println("\nThe length of the string without the period is " + (
charString.length()-1));
System.out.println("The number of indexes already used is " + count + ".")
; //displays the number of indexes already used
System.out.println("The lowest index used is index a[" + ((a.length-1)-(a.
length-1))); //displays the lowest index
System.out.println("The highest index used is index a[" + (a.length-1) + "
]."); //displays the highest index used
}
//POSTCONDITION:
//-------------------------------------------------------------------------
//EXIT PROGRAM
//PRECONDITION:
public void exitProgram()
{
//boolean repeat = true; //Need a (do while?) loop to continue the program
until the user wants to exit
System.out.println("\nThe program is now exiting.\n");
System.exit(0);
}
//POSTCONDITION:
//-------------------------------------------------------------------------
//MAIN MENU
//PRECONDITION:
public void mainMenu()
{
char choice;
do
{
System.out.println("\n\n\n-----------------------------------------------
-------------");
System.out.println("-----------------------------------------------------
-------");
System.out.println("------This is the main menu. Please make a selection
.------\n");
System.out.println("Please press [1] to input a character string.");
System.out.println("Please press [2] to display array info.");
System.out.println("Please press [3] to exit the program.");
choice = SavitchIn.readLineNonwhiteChar();
switch(choice)
{
case '1': checkInput();
break;
case '2': if(charString == null)
{
System.out.println("\nYou must use option 1 first.\n");
break;
}
displayArrayInfo(count); //gives NullPointerException if this is cho
sen first
break;
case '3': exitProgram();
break;
default: System.out.println("Options 1-3 only please.");
System.out.println("Please enter a selection.\n");
//break;
}
}while(choice != '3');
}
//POSTCONDITION:
//----------------------------------------------------------------------
//MAIN
//PRECONDITION:
public static void main(String[] args)
{
PalindromeCheck checkIt = new PalindromeCheck();
checkIt.mainMenu();
}
//POSTCONDITION:
}