2 Replies Latest reply: Oct 6, 2008 12:36 AM by 807589 RSS

    Please help. Trying to read in file to a 2d array.

    807589
      This is my code I can read in the file and print it back out. Thats no problem. The file is made up of vertices(Float) and faces(int) which need to be read in seperately
      into to different array types. I'm using 2d arrays, basArray1 for the vertices and basArray2 for the faces. The file looks like this:
      v
      0.0000000
      0.0000000
      0.0090090
      v
      9.0099833
      6.0994003
      0.3493489
      f
      1
      910
      54
      f
      4
      54
      789

      In my code I save the vertices into Floats called value1,value2,value3, which works, but when I do this basArray[num][o]=value1 and so on, then try to print basArray1, I get all null. Same goes for my faces except I get all zeros.

      BTW num starts at 0 and is incremented. basArray1 is a 994 by 3 array.
      This is my code. How can I put my read in file into two 2d arrays? HELP. :(
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      
      package renderer;
      
      import java.util.*;
      import java.io.*;
      import java.util.Scanner;
      import java.awt.geom.Line2D;
      import java.awt.Graphics;
      import java.awt.*;
      import javax.swing.*;
      
      /**
       *
       * @author Degrion
       */
      public class Main {
          
          
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              
              Float[] value = new Float[3];
              int[] val = new int[3];
              float value1;
              float value2;
              float value3;
              int val1,val2,val3;
              int num=0;
              int num2=0;
              float one=1;
              int a,b,c;
              
              //Try all basic arrays
              Float basArray1[][] = new Float[995][3];
              int basArray2[][] = new int[1985][3];
              Float points[][] = new Float[994][3];
              Float points2[][] = new Float[994][2];
              
              try{
                  File file = new File("C:\\Gamemovies\\sphere.obj");
                  Scanner scanner = new Scanner(file);
                  
                  while (scanner.hasNext()){
                      String test = scanner.next();
                      if (test.equals("v")){
                          value1 = Float.parseFloat(scanner.next());
                          value2 = Float.parseFloat(scanner.next());
                          value3 = Float.parseFloat(scanner.next());
                          basArray1[num][0]=value1; 
                          basArray1[num][1]=value2;
                          basArray1[num][2]=value3;
                          num++;
                          //System.out.println(value1 + " " + value2 + " " + value3);
                          System.out.println(basArray1[num][0] + " " + basArray1[num][1] + " " + basArray1[num][2]);
                      }
                      if (test.equals("f")){
                          val1 = Integer.parseInt(scanner.next());
                          val2 = Integer.parseInt(scanner.next());
                          val3 = Integer.parseInt(scanner.next());
                          basArray2[num2][0]=val1;
                          basArray2[num2][1]=val2;
                          basArray2[num2][2]=val3;
                          num2++;
                          //System.out.println(val1 + " " + val2 + " " + val3);
                          //System.out.println(basArray2[num2][0] + " " + basArray2[num2][2] + " " + basArray2[num2][2]);
                      }
                      for (int i=0;i<994;i++){
                          //System.out.print(basArray1.length);
                          //System.out.println(basArray1[0] + " " + basArray1[i][1] + " " + basArray1[i][2]);
      }

      }

      scanner.close();
      }
      catch (FileNotFoundException e){
      e.printStackTrace();
      }
      }

      }
      Ignore all the System.out.prints. Thats how I test code.