Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA. Redo Text Processing program so that if the array index goes out of bounds

ID: 3667900 • Letter: J

Question

JAVA. Redo Text Processing program so that if the array index goes out of bounds when the program accesses the array letterCount, it throws and handles the ArrayIndexOutOfBoundsException.

import java.io.*;
public class CharacterCount
{
public static void main(String[] args)
throws FileNotFoundException, IOException
{
int lineCount = 0;
int[] letterCount = new int[26];
int next;
FileReader inputStream = new FileReader("text.txt");
PrintWriter outfile = new PrintWriter("textCh.out");
next = inputStream.read();
while (next != -1)
{
next = copyText(inputStream, outfile,
next, letterCount);
lineCount++;
next = inputStream.read();
} //end while loop
writeTotal(outfile, lineCount, letterCount);
outfile.close();
}
//Place the definition of the method copyText, chCount,
//and writeTotal as described earlier here.
}

Explanation / Answer

import java.io.*;

//create class character
public class character
{
   public static void main (String[] args)
   {
       // declare variables
        int lineCount = 0;
        int[] letterCount = new int[26];

       // create class IntClasslass
        IntClass next = new IntClass();

        try
        {

            FileReader inputStream = new FileReader("text.txt");
            PrintWriter outfile =
                    new PrintWriter(new FileWriter("textCh.out"));

            next.setNum(inputStream.read());

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

            writeTotal(outfile, lineCount, letterCount);

            outfile.close();// close file
        }
       // throw exception
        catch (FileNotFoundException fnfe)
        {
            System.out.println("Exception: " + fnfe.toString());
        }
        catch (Exception e)
        {
            System.out.println("Exception: " + e.toString());
        }
    }

// copyText function
    static void copyText(FileReader infile, PrintWriter outfile,
                         IntClass next, int[] letterC) throws IOException
    {
          while (next.getNum() != (int)' ')
          {
              outfile.print((char)(next.getNum()));
              chCount((char)(next.getNum()), letterC);
              next.setNum(infile.read());
          }

          outfile.println();
    }

   // define function chCount
    static void chCount(char ch, int[] letterC)
    {
        int index;
        int i;

        ch = Character.toUpperCase(ch);
        index = (int) ch - 65;

        try
        {
            letterC[index]++;
        }
        catch (ArrayIndexOutOfBoundsException aiobe)
        {
            System.out.println("ArrayIndexOutOfBoundsException: "
                              + aiobe.toString());
        }
    }

   // define function writeTotal
    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]);
    }
}

IntClass.java

// create class IntClass
public class IntClass
{
   // declare variables
    private int x;

   // default constructor
    public IntClass()
    {
        x = 0;
    }

   // constructor with parameters
    public IntClass(int num)
    {
        x = num;
    }

   // setter
    public void setNum(int num)
    {
        x = num;
    }
  
   // getter
    public int getNum()
    {
        return x;
    }

    public void addToNum(int num)
    {
        x = x + num;
    }

    public void multiplyToNum(int num)
    {
        x = x * num;
    }

    public int compareTo(int num)
    {
        return (x - num);
    }

    public boolean equals(int num)
    {
        if(x == num)
          return true;
        else
          return false;
    }

    public String toString()
    {
        return (String.valueOf(x));
    }
}

text.txt
Hello there. How are you.

Today is a good day.

Summer is warm and winter is pleasant.


textCh.out

Hello there. How are you.

Today is a good day.

Summer is warm and winter is pleasant.

The number of lines = 3
A count = 8
B count = 0
C count = 0
D count = 4
E count = 7
F count = 0
G count = 1
H count = 3
I count = 4
J count = 0
K count = 0
L count = 3
M count = 3
N count = 3
O count = 6
P count = 1
Q count = 0
R count = 5
S count = 5
T count = 4
U count = 2
V count = 0
W count = 3
X count = 0
Y count = 3
Z count = 0