Re: Simple Java Question (reading text file of integer values)



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... "

.



Relevant Pages

  • Re: Can not compile
    ... Ting ... >> referenced by gActiveXObj. ... > the embedded 'count' member contained in the bounded array data type. ... > (First integer is the length, ...
    (microsoft.public.vb.general.discussion)
  • Re: Summing a function
    ... ending integer in A2 the funciton would look as follows: ... Let's see A1 for first integer, and B1 as last integer, use this array ...
    (microsoft.public.excel.worksheet.functions)