Re: Please help with my code
Quote:
the array when printed only has one number
That's exactly what the for loop is doing: it's putting temp (which was obtained from a line in the file) into every array slot.
Code :
for( int i = 0; i < n; i++)
{
arrs[i] = temp;
}
A for loop isn't appropriate. Rather you should assign the value of temp to some particular array position, which means worrying about which position.
Re: Please help with my code
I recommend using a List instead of an array for this operation, assuming you don't already know the number of entries that will be recorded from the file as input.
Code java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Stuff {
public static List fillarray(String filename)
{
List<Integer> arrs = new ArrayList<Integer>();
try
{
FileReader fr = new FileReader(filename);
BufferedReader inputStream = new BufferedReader(fr);
String line = null ;
while((line = inputStream.readLine()) != null)
{
System.out.println(line);
arrs.add(Integer.parseInt(line)) ;
}
inputStream.close();
}
catch(IOException e)
{
System.out.print("Error in Reading the file");
}
return arrs;
}
}
p.s. formatting is important, make it a habit now while your learning the language :-bd