Link to home
Start Free TrialLog in
Avatar of georges_nassar
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.read());

          while(next.getNum() != -1)
          {
              copyText(inputStream, outfile, next, letterCount);
              lineCount++;
              next.setNum(inputStream.read());
          }  // end while loop

          writeTotal(outfile,lineCount,letterCount);

          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+65) + " count = " + letters[i]);
    }
}

how to throws and handles the ArrayIndexOutOfBoundsException if the array index goes out of bounds when the program accesses the array letterCount
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of georges_nassar
georges_nassar

ASKER

i'm using textpad

i want to put a message ArrayIndexOutOfBoundsException when the array index goes out of bounds
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.toString() + " caught in main");
           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
I'm not sure if I understand... do you mean like this:

   throw new ArrayIndexOutOfBoundsException( "The error message that you want to display here." );

?
yes
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 );
        }
    }
}