Hi 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 ]
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.
Oops...one 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.
Joined: Jun 05, 2003
sorry .I don't understand what should i do with inFile 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 ]
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.
Joined: Jun 05, 2003
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.