When choosing the input file, I tried three ways & all of them generated errors !
I created the file.txt in the project's folder. I'm using Netbeans IDE.

1) Passing

java ShowFile Input.text

as a command-line argument [properties ---> run ---> wrote this command-line in the 'Argument' box]

2) Using the file's path....

File file = new File("C:\\Users\\TOSHIBA\\My Documents\\NetBeansProjects\\fin.txt");

3) Creating an instance of the file...

java.io.File file = new java.io.File("fin.txt");

This first one gave... "File Not Found" error. //FileNotFoundException
The second two gave... "Usage: ShowFile File" error. //ArrayIndexOutOfBoundsException

What's wrong with it?!

Thanks.

unless the value is fixed, I wouldn't add it to the arguments of netbeans. also: you can see that Input.text and fin.txt are not the same.
new File("fin.txt"); goes looking for a fin.txt file in the projects folder, not in the workspace.
an ArrayIndexOutOfBoundsException... well, if you don't know what that means, you are jumping into functionalities you are not ready for. working with arrays is a lot more basic than working with in and output files.

now, the ArrayIndexOutOfBoundsException is thrown whenever you are trying to access a non-existing element for an array. for example:

int[] intArr = new int[5];

the above line creates an array of 5 int primitives. now, the indices of Java start with 0, so, for every array you create, the possible indices vary from 0 to (n-1) where n is the number of elements in the array. for the above created array of int primitives, n is 5, so the possible indices are : 0 1 2 3 and 4.
so, you can get an element by doing:

int validIndex = 1;
int element = intArr[validIndex];

now, as long as validIndex has one of the above mentioned possible indices, there is no problem, but as soon as you try to get a value with an index below 0 or higher than (n-1), here, 5 or higher, you'll get an ArrayIndexOutOfBoundsException, because you are trying to get an element that is 'out of the bounds of the exception'.

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.