Results 1 to 4 of 4
- 10-25-2012, 11:42 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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:
If someone can also explain how showing int[] in the first line of a method is useful that would be great.Java Code:private int[] readInputData(String fileName) { }Last edited by jwl; 10-26-2012 at 04:29 PM.
- 10-26-2012, 03:37 PM #2
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: read a data file and storing it in an array
Ok, I have this:
It prints out 14 numbers starting with 13.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(); } }
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.
- 10-26-2012, 04:13 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: read a data file and storing it in an array
I think the method that does this should look something like this:
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?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]; } }
-
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
-
Storing data in Array List
By bluej in forum New To JavaReplies: 2Last Post: 01-30-2012, 05:24 AM -
Storing date in .data file
By Musaddict in forum New To JavaReplies: 2Last Post: 08-18-2011, 09:34 PM -
Storing data to file :s
By idi in forum New To JavaReplies: 3Last Post: 02-13-2010, 08:54 PM -
Readin from a file. and storing the information. (read inside)
By Rock-On in forum New To JavaReplies: 2Last Post: 11-29-2009, 10:26 AM -
Storing the data in a file
By vasavi.singh in forum New To JavaReplies: 4Last Post: 02-20-2009, 04:01 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks