Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Import Comma Delimited File to 2D Array

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Import Comma Delimited File to 2D Array

    Hi All,

    Goal: I will have varying sizes of a comma delimited file that I need to import into my Java program and store in a 2D array. Please note the examples will be much larger than the ones listed below. They will be on the order of 10,000 x 10,000.

    I have the code to import the comma delimited file, however I cannot seem to figure out a way to write the code to store it in a 2D array, and have the array size vary with the size of the file.


    Examples of comma delimited file that needs to go into a 2D array:
    #
    Name,1,2,3,4,5
    1,1,1,1,1,1
    2,1,1,1,1,0
    3,1,1,1,1,0
    4,1,1,1,1,1
    5,0,1,1,0,0
    6,1,1,1,1,0
    7,0,1,1,0,0

    Name,1,2,3,4,5,6,7,8,9,10
    1,1,1,1,1,1,1,1,1,1,1
    2,1,1,1,1,0,1,1,1,1,1
    3,1,1,1,1,0,0,0,1,1,1
    4,1,1,1,1,1,1,0,1,1,1
    5,0,1,1,0,0,1,0,1,0,0
    6,1,1,1,1,0,1,1,1,0,1
    7,0,1,1,0,0,1,1,0,0,1
    8,1,1,1,0,0,1,1,0,1,1
    9,1,1,1,1,0,1,1,1,1,1
    10,1,1,1,1,0,1,0,1,1,1
    11,1,0,0,0,0,0,0,1,0,0
    12,0,1,1,0,0,1,1,1,0,0
    13,0,1,0,0,0,1,1,0,0,0
    14,0,1,1,0,0,1,0,1,0,0
    15,1,1,1,0,0,0,0,1,0,1


    My current code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    public class CsvTest {
     
      public void readFile(String filePath 	) {
     
        BufferedReader br = null;
        int intExp;
     
        try {
     
          br = new BufferedReader(new FileReader(filePath));
          String line = null;
     
     
     
        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
        finally {
          try {
            if (br != null)
              br.close();
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
        }
      }
     
      public static void main(String[] args) {
        CsvTest test = new CsvTest();
        String path = "C:/comma_delim_test.csv";
        test.readFile(path);
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Import Comma Delimited File to 2D Array

    have the array size vary with the size of the file.
    Is there a direct relationship between the number of bytes in the file and the number of lines in the file?
    If so, you could compute the array size from the file size.
    If you read the first line, would that give you the size of each row and then file size / row size = nbr of rows
    Otherwise you could make a wild guess of the size to define your array
    OR you could use an ArrayList which will automatically expand as needed.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Import Comma Delimited File to 2D Array

    Great idea, I would never have thought of that. However, after some further thought, I can define the rows and columns at the start of the program, because I will know that information in advance.

    With some further tinkering and web searches I have code to put the file contents into an array, however it is a 1D array and it inputs it into the array as a string and I need int values. I put comments in the trouble spots.

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.lang.Integer;
     
    public class CsvTest {
     
      public void readFile(String filePath 	) {
     
        BufferedReader br = null;
     
        try {
     
          br = new BufferedReader(new FileReader(filePath));
          String line = null;
     
          while ((line = br.readLine()) != null) {
            //how do i input the contents into a 2D array?
            String [] values = line.split(",");
     
            for (String str : values) {
              //this is the code I'm trying to implement to convert the string to an int, but for some reason it does not work
              int intExp = Integer.valueOf(str);
              System.out.println(intExp);
            }
            System.out.println();
          }
        }
        catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
        finally {
          try {
            if (br != null)
              br.close();
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
        }
      }
     
      public static void main(String[] args) {
        CsvTest test = new CsvTest();
        String path = "C:/comma_delim_test.csv";
        test.readFile(path);
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Import Comma Delimited File to 2D Array

    The Integer class's parseInt method converts String to int

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Import Comma Delimited File to 2D Array

    I tried Interger.parseIng(str) too, however I still get an error.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Import Comma Delimited File to 2D Array

    What error? Please post the full text of the error message.
    What is the value of the str variable passed to the parseInt method?

  7. The Following User Says Thank You to Norm For This Useful Post:

    kigroy (January 15th, 2012)

  8. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Import Comma Delimited File to 2D Array

    Quote Originally Posted by Norm View Post
    What is the value of the str variable passed to the parseInt method?
    The value was "Name". Deleted the first line from the table and it works great now. Plus I think i have a way to get everything into a 2D array. Thanks for helping me through it.

Similar Threads

  1. Replies: 0
    Last Post: December 15th, 2011, 01:14 PM
  2. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  3. How to remove the last comma
    By fride360 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 07:20 AM
  4. how to import my own class in my .java file
    By amr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 09:31 PM
  5. how to import excel file to database table using java
    By palani in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2010, 12:17 AM