Reading a file into java and give it out
Hey,
i am new at Java. learned a bit in my last semester, but not very much and this semester, i suddenly have to do so much complicated thinks and i am not really good at it... what i have to do:
i have a file, called numbers.txt with one number in each line:
5
1
2
3
4
5
6
2
3
6
i have to read numbers.txt into an array and then they are saying give out the fifth field.
My main problem is actually that i don't know, if my code is right, because i have a macbook and i am using eclipse and i cannot read the file... i don't know how to do it exactly, actually more where i have to put the path... my macbook is never finding it... i hope that makes sense to you. my code looks like this:
public class searching
{
int field[];
private void reading(String filename)
{
int line = 0;
try
{
BufferedReader in = new BufferedReader(new FileReader(filename));
while(in.readLine() != null)
line++;
in.close();
int n = 0;
field[1] = n;
System.out.println(n);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
throws IOException
{
reading(String filename);
}
}
it would be very nice if anyone could help me, because i am not very sure about it...
little_polarbear
It is still not working :-(
Hey,
it still dosn't work... don't know why. It is not accepting my path. Is maybe my path wrong. I put it in as
/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt
for username i put my name in... and i put the path in my code on this place
private void addTo(int value, int[] array)
{
int n = array.length;
int[] temp = new int[n+1];
System.arraycopy(array, 0, temp, 0, n);
temp[n] = value;
fields = temp;
}
public static void main(String"/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt")
{
ReadAndSearch app = new ReadAndSearch();
String filePath = "readAndSearch.txt";
app.reading(filePath);
}
}
and if i am doing it, it is telling me, that there is the bracket before my main method wrong and after my main method... it is really strange... so where is my mistake....
little_polarbear