Hi.

So here's my code

public class TextFileIO
{
    static Console c;           // The output console
    static int total;

    public void askData ()
    {
        int line = 0;
        int i;
        c.print ("Enter the number of words you're going to input: ");
        total = c.readInt ();
        File dataInput = new File ("dataIn.text");

        String[] word = new String [total];

        do
        {
            try
            {
                FileWriter data = new FileWriter (dataInput);
                BufferedWriter dataBuff = new BufferedWriter (data);
                c.print ("Enter word" + (line + 1) + (": "));
                word [line] = c.readString ();
                line++;

            }
            catch (IOException e)
            {
                new Message ("Error");
            }

        }
        while (line >total);
        
        for (i = total; i >0; i--)
        {
            c.print (word[i]);
        }
    }

My problem is that this code shows an error? java.lang.ArrayIndexOutOfBoundsException?

for (i = total; i >0; i--)
        {
            c.print (word[i]);
        }

Edited 4 Years Ago by Daigan: n/a

ArrayIndexOutOfBoundsException?

Your index is out of bounds.
Print out the length of the array where you are having the problem and compare its size with the index value shown in the error message.

Remember arrays are zero based. The max index is the length - 1

Edited 4 Years Ago by NormR1: n/a

for (i = line ; i > 0 ; i = i - 1)
            {
              c.println (i);
            }

So I tried this, and it works.

Yes in a reverse order.

Edited 4 Years Ago by Daigan: n/a

Right, sorry about that. I think that it gets all the elements.

int total;
        int i;
        int line = 0;

        c.print ("Enter the number of words you're going to input: ");
        total = c.readInt ();

        String[] word = new String [total];
        do

            {
                c.print ("Enter word" + (line + 1) + (": "));
                word [line] = c.readString ();
                line++;
            }
        while (line < total);

        for (i = line ; i > 0 ; i = i - 1)
        {
            c.println (word [i]);
        }

Is this what you mean? It errors.

Here's the code:

// The "Testing" class.
import java.awt.*;
import hsa.Console;

public class Testing
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();
        int total;
        int i;
        int line = 0;

        c.print ("Enter the number of words you're going to input: ");
        total = c.readInt ();

        String[] word = new String [total];
        do

            {
                c.print ("Enter word" + (line + 1) + (": "));
                word [line] = c.readString ();
                line++;
            }
        while (line < total);

        for (i = line ; i > 0 ; i = i - 1)
        {
            c.println (word [i]);
        }

    } // main method
} // Testing class

Error message:

java.lang.ArrayIndexOutOfBoundsException: 5
at Testing.main(Testing.java:31)

The array on line 31 has less than 6 elements. Remember 0 based.

If you do not know the size of the array, print out its length.

Edited 4 Years Ago by NormR1: n/a

This question has already been answered. Start a new discussion instead.