Results 1 to 8 of 8
- 06-02-2008, 07:33 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-02-2008, 08:28 PM #2
readAndSearch.txtJava Code:import java.io.*; import java.util.Arrays; public class ReadAndSearch { // Start with an empty array. int[] fields = new int[0]; private void reading(String fileName) { try { File file = new File(fileName); BufferedReader in = new BufferedReader( new FileReader(file)); String line; // Read each line of the file, parse to an // [i]int[/i] and add it to the "fields" array. // When the reader reaches the end_of_file (EOF) // it returns "null" (see api). So we read until // we get a "null" return: while( (line = in.readLine()) != null ) { int n = Integer.parseInt(line); addTo(n, fields); } // Finished reading file, close it up. in.close(); } catch (IOException e) { System.out.println("read error: " + e.getMessage()); } // Print the "fifth field" which is the array // element located at index 4. System.out.println("fifth field value = " + fields[4]); // Show fields: System.out.printf("fields = %s%n", Arrays.toString(fields)); } 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[] args) { ReadAndSearch app = new ReadAndSearch(); String filePath = "readAndSearch.txt"; app.reading(filePath); } }
Java Code:5 1 2 3 4 5 6 2 3 6
- 06-02-2008, 08:44 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
still one little problem
Hey,
thank you so much for your fast answer, but i still have one problem...
i still don't find the file, which i wanna read... ecliplse is always saying
read error: readAndSearch.txt (No such file or directory)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ReadAndSearch.reading(ReadAndSearch.java:37)
at ReadAndSearch.main(ReadAndSearch.java:56)
and to be honest, i tried already so much, but it is not working, so how can i solve my problem.... is it because i have a macbook...
little_polarbear
- 06-03-2008, 03:09 AM #4
base on hardwired's code, the file "readAndSearch.txt" should be modified beside the ReadAndSearch class.
try to check if that txt's path is the same as ReadAndSearch class' path...
No....is it because i have a macbook...Last edited by sukatoa; 06-03-2008 at 03:11 AM.
freedom exists in the world of ideas
- 06-03-2008, 04:56 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
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
- 06-03-2008, 07:55 PM #6
That is nearly looks like casting a String to String...public static void main(String"/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt")
Declaring constant as parameter? the compiler doesn't allow that. maybe, Passing a constant to method's parameter....
If the path of readAndSearch.txt isReadAndSearch app = new ReadAndSearch();
String filePath = "readAndSearch.txt";
app.reading(filePath);(A linux/unix platform? or Solaris?)/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt
filepath should be assigned with "/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt"freedom exists in the world of ideas
- 06-04-2008, 09:35 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
Huhuuuuuuuuuuuu
It is working, thx so much for ur help :):):):):):):)
little_polarbear
- 06-05-2008, 10:32 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So it's better mark the thread as solved. :)
Similar Threads
-
Reading a file
By mew in forum New To JavaReplies: 2Last Post: 12-30-2007, 12:23 PM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM -
Reading a file for use
By peachyco in forum New To JavaReplies: 2Last Post: 11-27-2007, 03:49 AM -
Question abt.reading xml file using java
By gvi in forum Advanced JavaReplies: 6Last Post: 11-08-2007, 05:48 PM -
Reading from a file
By leebee in forum New To JavaReplies: 1Last Post: 07-23-2007, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks