| | |
Reading an array from a file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
How do I initialize an array that has been called from a file? There will be 5 values in the array. So should my file "input.dat" have:
5 (number of values)
4 (value)
6 (value)
7 (value)
2 (value)
7 (value)
Example:
BufferedReader br = new BufferedReader(
new FileReader("input.dat"));
and I'm lost after that.
Thanks,
Bud
5 (number of values)
4 (value)
6 (value)
7 (value)
2 (value)
7 (value)
Example:
BufferedReader br = new BufferedReader(
new FileReader("input.dat"));
and I'm lost after that.
Thanks,
Bud
•
•
Join Date: Mar 2005
Posts: 13
Reputation:
Solved Threads: 0
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
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
take a long at java.io.ObjectOutput as this has already been created for you...
http://java.sun.com/j2se/1.5.0/docs/...ectOutput.html
no sense reinventing the wheel
http://java.sun.com/j2se/1.5.0/docs/...ectOutput.html
no sense reinventing the wheel
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Reading numbers from a text file (Java)
- Program does not read through correctly (C)
- Trying to creating an array from a text file (C++)
- Help with a 2D array from a text file (C++)
- Getting an array from a txt file (C)
- Help Reading Info in Text File Into an Array (C++)
- problem reading text file to struct (C++)
Other Threads in the Java Forum
- Previous Thread: problem
- Next Thread: decrypting
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor





