Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java file reading and array help?

Name: Anonymous 2011-09-11 23:43

Hello all,

I need to read a data file (specified in command line when launching the program) and load the values from the file into a 2D array.
The data file will be two columns with an undetermined number of rows.

Example
2001     123987
2002     124678
2003     126098
etc.

I'm not quite sure how to define that array (can I analyze the file somehow before creating the array so the program knows how many rows there are?) If not, then I will just set the value of rows to 50.
Since the file has to be specified in the CL at launch, the only way I know to call the file is by using args as an identifier.
like if I have

      for (int i = 0; i < args.length; i++)
        System.out.println("Input file: " + args[i]);

then my program will output "Input file: myfile.data" for "C:\Anon\> Java MyProgram myfile.data"

How could I set up a for loop with the input scanner or something to fill an array with the values from the specified file?

I'm very novice at this, thank you so much for your help!

Name: Anonymous 2011-09-12 2:00


for (int i = 0; i < args.length; i++) {
    File file = new File(args[i]);
    BufferedReader reader = new BufferedReader(new FileReader(file));

    String line = null;
    while ((line = reader.readLine()) != null) {
         // process line
         // insert line to array
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List