Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-02-2008, 09:33 PM
Member
 
Join Date: Jun 2008
Posts: 50
little_polarbear is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-02-2008, 10:28 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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 // 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
Code:
5 1 2 3 4 5 6 2 3 6
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-02-2008, 10:44 PM
Member
 
Join Date: Jun 2008
Posts: 50
little_polarbear is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-03-2008, 05:09 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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...

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-03-2008, 06:56 PM
Member
 
Join Date: Jun 2008
Posts: 50
little_polarbear is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-03-2008, 09:55 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
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....

Quote:
ReadAndSearch app = new ReadAndSearch();
String filePath = "readAndSearch.txt";
app.reading(filePath);
If the path of readAndSearch.txt is
Quote:
/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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-04-2008, 11:35 AM
Member
 
Join Date: Jun 2008
Posts: 50
little_polarbear is on a distinguished road
Huhuuuuuuuuuuuu
It is working, thx so much for ur help


little_polarbear
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-05-2008, 12:32 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file mew New To Java 2 12-30-2007 02:23 PM
Reading text file Lennon-Guru New To Java 1 12-16-2007 01:38 AM
Reading a file for use peachyco New To Java 2 11-27-2007 05:49 AM
Question abt.reading xml file using java gvi Advanced Java 6 11-08-2007 07:48 PM
Reading from a file leebee New To Java 1 07-23-2007 02:02 PM


All times are GMT +3. The time now is 12:53 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org