?
Solved

ArrayIndexOutOfBoundsException

Posted on 2005-04-24
6
Medium Priority
?
530 Views
Last Modified: 2012-05-05
//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
0
Comment
Question by:georges_nassar
  • 3
  • 3
6 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 600 total points
ID: 13854597
Can you show the stack trace? By running this code:

import java.io.*;

public class CharacterCount
{
    public static void main(String[] args)
   {
       try
       {
          int lineCount = 0;
          int[] letterCount = new int[26];

          IntClass next = new IntClass();

        FileReader inputStream = new FileReader("1.txt");
          PrintWriter outfile =
                  new PrintWriter(new FileWriter("2.txt"));

          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();
       } catch ( Exception e )
       {
              e.printStackTrace( System.out );
       }
    }


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


Then, run the following the Command Prompt:

   java CharacterCode>stackTrace.txt& notepad stackTrace.txt

That will run the code, and output everything (including the stack trace) into a file, and then open the file into Notepad, so that you can then copy and paste the Stack Trace here.  :-)   (That's assuming you're on Windows?).

Regards;
0
 

Author Comment

by:georges_nassar
ID: 13863013
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
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13866085
I'm not sure if I understand... do you mean like this:

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

?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:georges_nassar
ID: 13867340
yes
0
 

Author Comment

by:georges_nassar
ID: 13867349
how to do it
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13868760
..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 );
        }
    }
}

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question