 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
TikkieTerug Guest
|
Posted: Thu May 26, 2005 6:37 pm Post subject: Simple Java Question (reading text file of integer values) |
|
|
For a project, we need to read a text file, that contains an integer
value on every line. We would like to store the integer values in an
array (the first integer being stored in arrayname[1]). Does anybody
have an idea how to write such a program?
We are grateful for any help we get! Thank you very much in advance.
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Thu May 26, 2005 6:58 pm Post subject: Re: Simple Java Question (reading text file of integer value |
|
|
On 26 May 2005 11:37:49 -0700, TikkieTerug wrote:
| Quote: | For a project, we need to read a text file, that contains an integer
value on every line. We would like to store the integer values in an
array (the first integer being stored in arrayname[1]).
|
What do you intend doing with element arrayname[0]?
| Quote: | ...Does anybody have an idea how to write such a program?
|
Yes.
| Quote: | We are grateful for any help we get!
|
Feel free to drop by once you formulate a specific question.
But please review your course notes, hack out some code[1],
and read the PhySci FAQ[2] carefully before replying.
[1] Have you done the HelloWorld program?
[2] <http://www.physci.org/codes/javafaq.jsp>
| Quote: | ..Thank you very much in advance.
|
You're very much welcome.
--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
|
|
| Back to top |
|
 |
Fahd Shariff Guest
|
Posted: Fri May 27, 2005 2:58 pm Post subject: Re: Simple Java Question (reading text file of integer value |
|
|
Here is a quick hack. The lines are read into an arraylist which is
then copied to an array. The first integer is stored in arr[0] i.e.
integer k is stored in arr[k-1]. If thats not what you want, you can
fix it easily.
BufferedReader in = new BufferedReader(new
FileReader("data.txt")) ;
String line = "" ;
ArrayList nums = new ArrayList() ;
while((line = in.readLine())!=null){
int num ;
try{
num = Integer.parseInt(line) ;
nums.add(new Integer(num));
}catch(NumberFormatException e){}
}
int[] arr = new int[nums.size()] ;
for(int i = nums.size() ; --i>=0 {
arr[i] = ((Integer)nums.get(i)).intValue() ;
}
--
Fahd Shariff
"Let the code do the talking... "
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|