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

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

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

So I tried this, and it works.

Are you trying to print out all of the contents of the array?

Yes in a reverse order.

Does your code get all the elements?

Sorry, but what are elements in Java?

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

Print them out with for loop and see.
I think you are missing one.

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.

You should post the full text of the error message if you want help with it.

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.

Oh, I totally forgot about that. Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.