User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,159 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,841 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting

Reading an array from a file

Join Date: Mar 2005
Posts: 13
Reputation: Bud4java is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Bud4java Bud4java is offline Offline
Newbie Poster

Re: Reading an array from a file

  #2  
May 5th, 2005
Some progress has been made...

I've been able to create the text file with data, and create the array. The thing that I cannot get it to do is to use the first number in the file as the size of the array. The array in the text file is a set of scores, and there are 5 scores. The first number is supposed to set the size of the array, so the first number is 5, followed by the actual 5 values in the array.

Is this possible? If so, how would I setup the declaration/initialization statement?
Right now I have:

BufferedReader fr = new BufferedReader (new FileReader ("goals.txt"));int score [] = new int [5];
// read all scores from the files goals.txt
for (int i = 0 ; i < 5 ; i++)
{

goal [i] = Integer.parseInt(fr.readLine ());
}
// display the nine goals on the screen
System.out.print ("The goals are: ");
for (int i = 0 ; i < 5; i++)
{
if (i < 4)
// add a comma if not the last hole
System.out.print (score [i] + ", ");
else
// no comma and a prinln for the last output
System.out.println (score [i]);
} // end loop




The text file contains:
5
99
88
79
98
100

but it still wants to consider the "5" as part of the data in the array instead of it being the size of the array.

Any suggestions?


Thanks for your help & consideration,
Bud
Reply With Quote  
All times are GMT -4. The time now is 8:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC