We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,201 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading from a text file

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.

2
Contributors
1
Reply
1 Hour
Discussion Span
7 Months Ago
Last Updated
2
Views
Question
Answered
sapure
Newbie Poster
21 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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'.

stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
Question Answered as of 7 Months Ago by stultuske

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0613 seconds using 2.7MB