title: How to read double values from a txt file
body: <p>I have a class 'DataReader' with some variables and I want to put in them the values from a file. </p>
  
  <p>For the budget (the first line of the txt file),; the number of projects (the second line); and the number of departments (the third line) there are no problems. </p>
  
  <p>The problems are when I'm trying to fill in the array of doubles.</p>
  
  <p>Other problem I'm finding is to set the length of the array. I put in this example 4000 because the file I have is of 4003 lines (the first 3 are budget, # projects and # departments). but actually the array is supposed to be as long as the length of the file (which won't necessarily be 4000)</p>
  
  <pre><code>    public DataReader() {
              this.budget = 0;
              this.numberOfDepartments = 0;
              this.numberOfProjects = 0;
              this.array = new double[4000][this.numberOfProjects];
      }
  
      public void readDataFromFile() throws FileNotFoundException {
              String string = null;
              FileReader file = new FileReader("C:/Users/Andy/Desktop/myfile.txt");    //open a communication buffer with the txt file
              Scanner reader = new Scanner(file);    //Initialize a scanner to read the txt file
  
              /* READ THE BUDGET */
              string = reader.nextLine();   //read the first line
              this.budget = Integer.parseInt(removeOneCommaFromString(string)); //remove the comma and transform it in an integer
              System.out.println(this.budget);        //test
  
              /* READ THE NUMBER OF DEPARTMENTS */
              string = reader.nextLine();
              this.numberOfDepartments = Integer.parseInt(removeOneCommaFromString(string));
              System.out.println(this.numberOfDepartments);
  
              /* READ THE NUMBER OF PROJECTS */
              string = reader.nextLine();
              this.numberOfProjects = Integer.parseInt(removeOneCommaFromString(string));
              System.out.println(this.numberOfProjects);
  
              /* READ THE MATRIX */
              for(int i = 0; i &lt; 4000; i++) {
                      string = reader.nextLine();
                      string = removeCommasFromString(string);
                      char[] temp = string.toCharArray();
                      char[] temp2 = new char[temp.length];
                      for(int z = 0; z &lt; this.numberOfProjects; z++) {
                              for(int j = 0; j &lt; temp.length; j ++) {
                                      if(temp[j] &gt;= 0 || temp[j] &lt;= 9 || temp[j] == '.') {
                                              temp2[j] = temp[j];
                                      }
                                      else {
                                              String newString = new String(temp2);
                                              Double number = new Double(newString);
                                              array[i][z] = number;
                                              System.out.println(array[i][z]);
                                      }
                              }
                      }
              }
  
      }
  
      private String removeCommasFromString(String string) {
              char[] stringWithoutCommas = new char[string.length()];
              char[] stringWithCommas = string.toCharArray();
  
              for(int i = 0; i &lt; stringWithCommas.length; i++) {
                      if(stringWithCommas[i] != ',') {
                              stringWithoutCommas[i] = stringWithCommas[i];
                      }
                      else {
                              stringWithoutCommas[i] = ' ';
                      }
              }
              String correctString = new String(stringWithoutCommas);
              return correctString;
      }
  
      private String removeOneCommaFromString(String string) {
              // TODO Auto-generated method stub
              char[] array1 = string.toCharArray();
              char[] array2 = new char[array1.length -1];
  
              for(int i = 0; i &lt; array1.length-1; i++) {
                      array2[i] = array1[i];
              }
  
              String stringWithoutComma = new String(array2);
              return stringWithoutComma;
      }
  
      public static void main(String[] args) throws FileNotFoundException {
              DataReader ld = new DataReader();
              ld.readDataFromFile();
      }
  }
  </code></pre>
  
  <p>This is a sample of the txt file I'm reading from, in which the first line is the budget, the second one is the number of departments and the third one is the number of projects:</p>
  
  <pre><code>3420,
  2,
  10,
  103.8421,1250.434,54.56157,36.92909,962.7694,173.022,106.7644,404.1759,11.66381,2966.675,
  0.1270486,0.1229978,0.06825768,0.2493465,0.1555537,0.05003887,0.2638902,0.1703413,0.2503172,0.1844837,
  104.5753,446.7093,825.9758,1220.173,726.259,508.811,362.4495,758.9695,258.2516,1955.544,
  0.05048114,0.07712628,0.1827393,0.06109419,0.1818522,0.1821966,0.1377791,0.07180541,0.07752416,0.06139474,
  444.7019,461.5938,976.8734,273.714,184.7028,229.7422,130.7461,84.34789,49.8125,417.2332,
  0.1141632,0.06448551,0.08264564,0.08626967,0.2759337,0.2762382,0.1682017,0.2879532,0.1430924,0.2055033,
  53.11755,479.1441,49.23315,188.405,1127.118,117.5416,105.4314,78.87017,50.0128,719.333,
  </code></pre>
  
  <p><a href="http://pastebin.com/gCUA3PCc" rel="nofollow">This is the full file</a></p>