Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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);
        }
    }
    readAndSearch.txt
    Java Code:
    5
    1
    2
    3
    4
    5
    6
    2
    3
    6

  3. #3
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default 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

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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....
    Last edited by sukatoa; 06-03-2008 at 03:11 AM.
    freedom exists in the world of ideas

  5. #5
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default 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

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    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"
    freedom exists in the world of ideas

  7. #7
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default Huhuuuuuuuuuuuu

    It is working, thx so much for ur help :):):):):):):)


    little_polarbear

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 12:23 PM
  2. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM
  3. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 03:49 AM
  4. Question abt.reading xml file using java
    By gvi in forum Advanced Java
    Replies: 6
    Last Post: 11-08-2007, 05:48 PM
  5. Reading from a file
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •