Results 1 to 2 of 2

Thread: array help

  1. #1
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Unhappy array help

    I am trying to figure out ow to read integers from a user specified file in to an array. Since i don't ave any information about te file except for the fact that it is more or less a matrix, how do I get the program to read those numbers into a multidimensional array ?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You will HAVE to know something about the file. For example, you'll need to know what delimiters are being used, or test for several different ones; i.e. spaces, commas, periods, whatever.

    Once you know what type of delimiter is used, you can make a 2d array and then read the input line by line.
    For file input, I usually use something like the following:
    Java Code:
    BufferedReader in = new BufferedReader(new FileReader("myFile.txt"));
    String line = in.readLine();
    String[] numbersAsStrings = line.split(",");  //creates an array of strings split by the supplied delimiter, in this case a ,
    for(int i=0; i<numbersAsStrings.length; i++){
        some2DArray[row][i] = Integer.parseInt(numbersAsStrings[i]);
    }
    That is an over-simplified example, I'm not checking for errors, and I'm only reading in 1 row. You would need to either make a nested for loop or a while loop that tests for end of file or something like that. I hope it gets you started! :D

Similar Threads

  1. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  2. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  3. array or array list?
    By Drukqs in forum New To Java
    Replies: 5
    Last Post: 05-06-2009, 06:37 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM

Posting Permissions

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