georges_nassar
asked on
ArrayIndexOutOfBoundsException
//Program: Line and letter count
import java.io.*;
public class CharacterCount
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
int lineCount = 0;
int[] letterCount = new int[26];
IntClass next = new IntClass();
FileReader inputStream = new FileReader("F:\\text.txt") ;
PrintWriter outfile =
new PrintWriter(new FileWriter("F:\\textCh.out "));
next.setNum(inputStream.re ad());
while(next.getNum() != -1)
{
copyText(inputStream, outfile, next, letterCount);
lineCount++;
next.setNum(inputStream.re ad());
} // end while loop
writeTotal(outfile,lineCou nt,letterC ount);
outfile.close();
}
static void copyText(FileReader infile, PrintWriter outfile,
IntClass next, int[] letterC) throws IOException
{
while(next.getNum() != (int)'\n')
{
outfile.print((char)(next. getNum())) ;
chCount((char)(next.getNum ()), letterC);
next.setNum(infile.read()) ;
}
outfile.println();
}
static void chCount(char ch, int[] letterC)
{
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)
{
int i;
outfile.println("The number of lines = " + lines);
for(i = 0; i < 26; i++)
outfile.println((char)(i+6 5) + " count = " + letters[i]);
}
}
how to throws and handles the ArrayIndexOutOfBoundsExcep tion if the array index goes out of bounds when the program accesses the array letterCount
import java.io.*;
public class CharacterCount
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
int lineCount = 0;
int[] letterCount = new int[26];
IntClass next = new IntClass();
FileReader inputStream = new FileReader("F:\\text.txt")
PrintWriter outfile =
new PrintWriter(new FileWriter("F:\\textCh.out
next.setNum(inputStream.re
while(next.getNum() != -1)
{
copyText(inputStream, outfile, next, letterCount);
lineCount++;
next.setNum(inputStream.re
} // end while loop
writeTotal(outfile,lineCou
outfile.close();
}
static void copyText(FileReader infile, PrintWriter outfile,
IntClass next, int[] letterC) throws IOException
{
while(next.getNum() != (int)'\n')
{
outfile.print((char)(next.
chCount((char)(next.getNum
next.setNum(infile.read())
}
outfile.println();
}
static void chCount(char ch, int[] letterC)
{
int index;
int i;
ch = Character.toUpperCase(ch);
index = (int) ch - 65; //Step b
if(index >= 0 && index < 26) //Step c
letterC[index]++;
}
static void writeTotal(PrintWriter outfile, int lines,
int[] letters)
{
int i;
outfile.println("The number of lines = " + lines);
for(i = 0; i < 26; i++)
outfile.println((char)(i+6
}
}
how to throws and handles the ArrayIndexOutOfBoundsExcep
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not sure if I understand... do you mean like this:
throw new ArrayIndexOutOfBoundsExcep tion( "The error message that you want to display here." );
?
throw new ArrayIndexOutOfBoundsExcep
?
ASKER
yes
ASKER
how to do it
..How to do what? :o\
What is it exactly that you want to achieve here?
btw, if you only want to count the number of characters in a file, and then output the result to another file, then it's as simple as this:
import java.io.*;
public class CharacterCount
{
private String inputFile = "F:\\text.txt";
private String outputFile = "F:\\textCh.out";
public CharacterCount() {}
public static void main( String [] args )
{
new CharacterCount().count();
}
public void count()
{
try
{
BufferedWriter out = new BufferedWriter( new FileWriter( outputFile ) );
out.write( "" + (new File( inputFile )).length() );
out.close();
} catch ( Exception e )
{
e.printStackTrace( System.out );
}
}
}
What is it exactly that you want to achieve here?
btw, if you only want to count the number of characters in a file, and then output the result to another file, then it's as simple as this:
import java.io.*;
public class CharacterCount
{
private String inputFile = "F:\\text.txt";
private String outputFile = "F:\\textCh.out";
public CharacterCount() {}
public static void main( String [] args )
{
new CharacterCount().count();
}
public void count()
{
try
{
BufferedWriter out = new BufferedWriter( new FileWriter( outputFile ) );
out.write( "" + (new File( inputFile )).length() );
out.close();
} catch ( Exception e )
{
e.printStackTrace( System.out );
}
}
}
ASKER
i want to put a message ArrayIndexOutOfBoundsExcep
can i put a message inside the method without using the method printStackTrace
i don't have stackTrace.txt
i found this:
import java.io.*;
public class PrintStackTraceExample1
{
public static void main (String[] args)
{
try
{
methodA();
}
catch(Exception e)
{
System.out.println(e.toStr
e.printStackTrace();
}
}
static void methodA() throws Exception
{
methodB();
}
static void methodB() throws Exception
{
methodC();
}
static void methodC() throws Exception
{
throw new Exception("Exception generated in method C");
}
}
how can i do my program the same like it