|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-02-2008, 09:33 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 50
|
|
|
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, 10:28 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
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
// int 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);
}
}
readAndSearch.txt
|
|

06-02-2008, 10:44 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 50
|
|
|
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, 05:09 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
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...
is it because i have a macbook...
No....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by sukatoa : 06-03-2008 at 05:11 AM.
|
|

06-03-2008, 06:56 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 50
|
|
|
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, 09:55 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
public static void main(String"/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt")
That is nearly looks like casting a String to String...
Declaring constant as parameter? the compiler doesn't allow that. maybe, Passing a constant to method's parameter....
ReadAndSearch app = new ReadAndSearch();
String filePath = "readAndSearch.txt";
app.reading(filePath);
If the path of readAndSearch.txt is
/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt
(A linux/unix platform? or Solaris?)
filepath should be assigned with "/Users/username/Eclipse/ReadandSearch/ReadandSearch.txt"
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-04-2008, 11:35 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 50
|
|
|
Huhuuuuuuuuuuuu
|
|

06-05-2008, 12:32 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
So it's better mark the thread as solved. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|