| Author |
A little help working with a file object
|
Chas Martin
Greenhorn
Joined: Oct 07, 2008
Posts: 18
|
|
Hey everybody, Java beginner here! Dont worry, im not asking anyone to do my homework for me lol. I am having troubles with netbeans working with a file object. My project requires me to save a .txt file to my project directory and then have the data used in my program. The file is a list of countries along with 2 numerical values showing the change in energy usage in the years 1984 and 2004. Here is an example of the txt file: austria 13.4 10.1 Brazil 14.4 9.8 and so on, the list contains about 65 different countries. I have created a buffered reader and assigned a number to each line in the file. I have it now so that when I compile, it shows the line number starting at 1 and the contents of that line from the file. Example: run: 1 : austria 2 : 13.4 3 : 10.1 4 : 5 : brazil and so on The thing that is holding me up is I dont know how to differentiate the lines between the country names and the the values. I need to do this because I need to perform a simple math equation using both values for each country. I need a hint as to what I should do to make the numerical values usable and identifiable so i can calculate the percent change between the two values for each country. Thanks a lot folks, Any little hints would be greatly appreciated because I am stuck real good here. Thanks, Chas
|
 |
Richard Chambers
Ranch Hand
Joined: Apr 03, 2008
Posts: 40
|
|
I probably know less than you do, but I'll post anyway with the advise of listening to the wiser ones. I'd make the txt file a CSV file, and use an array to access it. Just my thoughts.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3041
|
|
It looks like your data is always in the format: String double double null Since you know which lines are going to be doubles (the 2 & 3 out of every 4 lines) then all you need to do is read in the String for that line (you seem to already have that) and convert the value to a numeric double. There are a couple ways of doing this, but the easiest comes from the Double class. Here is the API: JavaSE 6 API: Double class Can you see the method to use? Getting to know the API and how to use it is key to learning how to effectively program in Java.
|
Steve
|
 |
Chas Martin
Greenhorn
Joined: Oct 07, 2008
Posts: 18
|
|
Thanks for the great advice Steve, very handy. How do you assign the string double double null to their respective lines? this is where I am stuck now. I think that once i figure that out, I will have no prob piecing together the rest of my project
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
Welcome to JavaRanch Have you come across any classes with methods like nextDouble()? If not, look through the index for the API documentation. You only have 4 to choose from and the descriptions should help you narrow down the choice.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3041
|
|
Originally posted by Chas Martin: Thanks for the great advice Steve, very handy. How do you assign the string double double null to their respective lines? this is where I am stuck now. I think that once i figure that out, I will have no prob piecing together the rest of my project
How are you reading in the lines? You said you are using BufferedReader correct? Which method are you using? Once you have the lines, how are you storing them? It would be best if you store them in an ordered collection like a list or an array. If you are storing them in an ordered list you can then traverse the list in order and use your index to determine what value should be in the list. If not you could do the conversion right at the point that you read the line in from the file (which I would choose myself). Either run the file reading in a loop that reads 4 lines at a time or keep a counter that you mod by 4 to get which line you are working on and there-fore which value should be in the String: The method that Campbell points to above is a Real Nice way of doing things. The only reason I took this route was because it is closer to what you describe in your first post as using a BufferedReader.
|
 |
 |
|
|
subject: A little help working with a file object
|
|
|