I have to create a program that reads the text, outputs the text as is and prints the number of lines and the number of times each letter appears in the text. I have to include an exception, so that if the array index goes out of bounds when the program accesses the array letterCount, it throws and handles the ArrayIndexOutOfBoundsException. I have the program completed and working , but I'm having trouble adding the exception to the program. Here is my code:
Hi it's me again. Sorry for not checking what you wrote, I was only looking at the code you gave. It seems that I suggested not to use ArrayIndexOutOfBoundsException even though that is what you wanted.
Who told you to use this exception handling? Because what I suggested was generally correct: you don't want to catch such exception, but instead make sure it doesn't happen by checking the length of the array.
Anyway if you want ArrayIndexOutOfBoundsException here it is.:
static void chCount(char ch, int[] letterC) throws ArrayIndexOutOfBoundsException {
int index;
int i;
ch = Character.toUpperCase(ch); //Step a
index = (int) ch - 65; //Step b
if (index >= 0 && index < 26) //Step c
letterC[index]++;
}
static void writeTotal(PrintWriter outfile, int lines,
int[] letters)
throws ArrayIndexOutOfBoundsException {
int i;
outfile.println();
outfile.println("The number of lines = " + lines);
for (i = 0; i < 26; i++)
outfile.println((char)(i + 65) + " count = "
+ letters[i]);
}
static void copyText(FileReader infile, PrintWriter outfile,
IntClass next, int[] letterC) throws IOException, ArrayIndexOutOfBoundsException
{
while (next.getNum() != (int)'\n')
{
outfile.print((char)(next.getNum()));
chCount((char)(next.getNum()), letterC);
next.setNum(infile.read());
}
outfile.println();
}
When you add throws to a method (methodA) declaration, if an exception of that kind occurs in the method then it will not give you trouble inside the method. You will not have to put try-catch inside the method. But instead you throw it to the method that called methodA.
The above method copyText has a throws because it calls chCount which throws an ArrayIndexOutOfBoundsException .
It depends where you want to handle it:
1st version:
void A(String s) {
try {
int i = Integer.parseInt(s);
// do other stuff
// perhaps return something if the method was not void
} catch(NumberFormatException nfe) {
//handle it here
//print a message
}
}
void B() {
A("3");
A("a");
}
Or Version 2:
void A(String s) throws NumberFormatException {
int i = Integer.parseInt(s);
}
void B() {
try {
A("3");
} catch (NumberFormatException nfe) {
//handle it here
}
try {
A("a");
} catch (NumberFormatException nfe) {
//handle it here
}
}
So now that you have added the throws ArrayIndexOutOfBoundsException, go to where you call the methods and put them in a try-catch. Since in my suggestions I have already put your code in try-catch, simply add one more catch() { }
And please tell me who told you to use ArrayIndexOutOfBoundsException? And sorry for confusing you and for my many posts