This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes can not open the file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can not open the file" Watch "can not open the file" New topic

can not open the file

karam salem

Joined: Jun 05, 2003
Posts: 3
my program must open the file "me.txt" which contains numbers (number in each line)
then it must insert its contents into an array of float type.but it has some errors i could not fix them
i hope that you can help me
here is my code[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ June 05, 2003: Message edited by: Dirk Schreckmann ]
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

You might want to look at TestFileIn as provided by the JavaRanch Common Package. You might just use their package to read the data in or you could analyze the source code found here
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

OK, A couple of things here:
1). In Open.main, you have the line:

This will not compile, as float is not a class. You want Float (notice the capital letter). To be less confusing to a beginning Java reader, you could specify the whole package:

(While people can confuse float and Float, they are less likely to confuse float and java.lang.Float.)
2). In Open, your array variable is never initialized. You declare it, but never assign it a value. In Java, arrays are objects and as such need to be instantiated. So, you would have to say:

Of course, that limits you to having only 10 entries in your text file. You could use a java.util.List and add java.lang.Floats to the list and then build your array from the list of Floats (sounds like a parade, "list of floats..." ) This would allow an unlimited number of entries in the text file. Consider how to modify your main method in Open to allow this.
3). Not a problem per se, but a style point. You have documented your code, but it is not up to Javadoc standard. This means that you would not be able to generate the Javadoc from this code. Javadoc comments start with /** and end with */ and are placed before the method, not between the method and the body. Example:

That's my comments for now.
BTW, welcome to JavaRanch, Karam.
[ June 05, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824 more thing:
In your Open class, main is a static method, which means that there is no instance associated with it. However, your inFile variable is an instance variable. You cannot access instance variables from a static context, because there is no instance from which to get the variable! In this case, you are best to get rid of the member variable entirely and make it a local variable in the main method.
karam salem

Joined: Jun 05, 2003
Posts: 3
sorry .I don't understand what should i do with
by the way,when I run the program it listed the file elements but after that the following line appear
Exception in thread "main" java.lang.NumberFormatException : empty String
at java.lang.FloatingDecimal.readJavaFormatString(compiled Code)
at java.lang.Float.parseFloat(Float:java:187)
at Open.main(Compiled Code)
[ June 05, 2003: Message edited by: karam salem ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Instead of saying:

You should say:

Note the difference: in the first, inFile is an instance variable belonging to an instance of the object; in the latter, inFile is a local variable existing only in the main method.
You are (probably) getting your NumberFormatException because you do not have 10 lines in your input file (me.txt). Again, you should probably use a list and a while() loop instead of a for loop. Try:

and see if that helps.
karam salem

Joined: Jun 05, 2003
Posts: 3
You are right
when i put 10 numbers in the file the program work without errors
i tried to use while istead of for loop
but same errors appear when the number is less than 10.How can i make the program more flexible?
thank you for your time.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: can not open the file
It's not a secret anymore!