Results 1 to 4 of 4
Like Tree1Likes

Thread: read a data file and storing it in an array

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default read a data file and storing it in an array

    I need to read a data file and store it in an array. It has to be an array because the first line has to indicate the number of data items to be read which is now 13.

    It needs to be read from a method that looks like this:

    Java Code:
    private int[] readInputData(String fileName) {
           
       }
    If someone can also explain how showing int[] in the first line of a method is useful that would be great.
    Last edited by jwl; 10-26-2012 at 05:29 PM.

  2. #2
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: read a data file and storing it in an array

    Ok, I have this:
    Java Code:
    package readfile;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
     
    public class ReadFile {
        private Scanner read;
     
       private void openFile() {
           try {
               read = new Scanner(new File("FIle.txt"));
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
      
       private void readFile() {
           while(this.read.hasNext()) {
                String a = read.next();
                System.out.printf("%s\n", a);
           }
       }
       
       private void closeFile() {
           read.close();
       }
       
        public static void main(String[] args) {
            ReadFile file = new ReadFile();
            file.openFile();
            file.readFile();
            file.closeFile();
        }
    }
    It prints out 14 numbers starting with 13.
    run:
    13
    105
    112
    105
    111
    112
    113
    107
    118
    108
    110
    109
    112
    108
    BUILD SUCCESSFUL (total time: 1 second)

    What I need to do is store all the numbers after 13 in an array of size 13 where the number 13 from the data file creates an array of size 13. Any help or direction will be great.

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: read a data file and storing it in an array

    I think the method that does this should look something like this:

    Java Code:
    private int[] readFile(String File) {
           while(this.read.hasNext()) {
                String a = read.next();
                System.out.printf("%s\n", a);
                return new int[File];
           }
       }
    Where "File" refers to the txt file but it says there is no return statement on line 1 of this code. On line 5 it has an error saying required int found String. I changed the readFile method a little but it's basically the same code as above. Am I going in the right direction here?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: read a data file and storing it in an array

    I wouldn't create a Scanner anywhere but inside of your readFile method since the File name String is passed in. What I would do, in pseudocode is something like:

    Java Code:
    method readFile accepts a fileName String parameter throws an IOException
      create new File, dataFile, with fileName
      create new Scanner object, fileScanner, with dataFile.
      use fileScanner to get first int, create an int array of this size
      use a for loop going from i = 0 to i < intArray.length to get ints with fileScanner and 
          place into int array. 
      close Scanner object.
      return int array.
    end method

Similar Threads

  1. Storing data in Array List
    By bluej in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 06:24 AM
  2. Storing date in .data file
    By Musaddict in forum New To Java
    Replies: 2
    Last Post: 08-18-2011, 10:34 PM
  3. Storing data to file :s
    By idi in forum New To Java
    Replies: 3
    Last Post: 02-13-2010, 09:54 PM
  4. Replies: 2
    Last Post: 11-29-2009, 11:26 AM
  5. Storing the data in a file
    By vasavi.singh in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 05:01 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
  •