Results 1 to 9 of 9
  1. #1
    almjodla is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Read File into 2d array

    Hi!
    Yepp iīm a beginner to java and I face a propably simple problem. I did some research on google but couldnīt find a solution for my problem. I would appreciate your help on that.

    Problem:
    Reading from a plain txt file into an double array

    In the original code the array was defined as follows:
    double[][] matrix = {{0,2.3,3.1,2.1 ... },
    {2,3,0,1.3,1.3 ... },
    {... }};

    What I want to do now is to read the data from a file instead of providing it directly in the code (there was a 65k something limitation dropped).

    File format text file
    0,2.3,3.1,2.1 ... Linebreak
    2,3,0,1.3,1.3 ... Linebreak
    ...
    ..

    The code if experimented with:
    Java Code:
      BufferedReader br = null;
    	    
    	    try {
    	      
    	      br = new BufferedReader(new FileReader("distancematrix_edited_for_import.txt"));
    	      String line = null;
    	      
    	      while ((line = br.readLine()) != null) {
    	        
    	        //String[] values = line.split(",");
    	        double[][] matrix = line.split(",");
    	        //Do necessary work with the values, here we just print them out
    	        for (String str : values) {
    	          System.out.println(str);
    	        }
    	        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();
    	      }
    	    }
    I found the above example on this website. There are some issues now with data types. I would appreciate your support for this problem.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The line double[][] matrix = line.split(","); won't compile because the String.split returns a String[] and you are trying to assign it to a double array. You'd have to convert each string in the String[] to a double using the Double.parseDouble method.

  3. #3
    almjodla is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default implemtation follows...

    ...iīm trying to implement that.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Well then you start with a double array, initialize it to the length of the string array.
    Then you use a loop which grabs a string from the string array, converts it to a double and sets the double value into the double array.

  5. #5
    almjodla is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default almost there...

    I did this and Iīm almost there: thx for the support!

    Java Code:
    BufferedReader br = null;
    	  double[][] matrix = new double[102][102];
    	  
    	  	    try {
    	      
    	      br = new BufferedReader(new FileReader("distancematrix_edited_for_import.txt"));
    	      String line = null;
    	      int x=0;
    	      int y=0;
    	      while ((line = br.readLine()) != null) {
    	        
    	        String[] values = line.split(",");
    	    	      
    	        for (String str : values) {
    	          
    	        	double str_double = Double.parseDouble(str);
    	        	matrix[x][y]=str_double;
    	        	System.out.println(str_double);
    	        }
    	       y=y+1;
    	      }
    	      x=x+1;
    	    }

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Well done!

  7. #7
    almjodla is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    Iīm also a bit proud of myself ^^
    Works perfect now. Awesome!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by r035198x View Post
    Well done!
    Sorry to spoil the fun but this:

    Java Code:
    while ((line = br.readLine()) != null) {
    	        
    	        String[] values = line.split(",");
    	    	      
    	        for (String str : values) {
    	          
    	        	double str_double = Double.parseDouble(str);
    	        	matrix[x][y]=str_double;
    	        	System.out.println(str_double);
    	        }
    	       y=y+1;
    	      }
    	      x=x+1;
    	    }
    ... doesn't really work: for all the values on the line the same matrix entry m[x,y] is assigned. Those loop increments are at the wrong place.

    kind regards,

    Jos

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    True, the y=y+1; needs to sneak into the for brace. Wonder if the OP has tested and corrected this already.
    One hopes they haven't deployed the code to production and then dashed off for the next flight to a holiday in the Bahamas.

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2009, 11:07 AM
  2. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  3. Replies: 5
    Last Post: 07-30-2008, 10:30 AM
  4. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 04:53 AM
  5. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 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
  •