Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 1.45 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java read values from text file
  2. BufferedReader
  3.        
  4. try {
  5.         FileInputStream fstream = new FileInputStream("input.txt");
  6.         DataInputStream in = new DataInputStream(fstream);
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  8.         String strLine;        
  9.         int lineNumber = 0;
  10.         double [] a = null;
  11.         double [] b = null;
  12.         // Read File Line By Line
  13.         while ((strLine = br.readLine()) != null) {
  14.             lineNumber++;
  15.             if( lineNumber == 4 ){
  16.                 a = getDoubleArray(strLine);
  17.             }else if( lineNumber == 5 ){
  18.                 b = getDoubleArray(strLine);
  19.             }              
  20.         }
  21.         // Close the input stream
  22.         in.close();
  23.         //print the contents of a
  24.         for(int i = 0; i < a.length; i++){
  25.             System.out.println("a["+i+"] = "+a[i]);
  26.         }          
  27.     } catch (Exception e) {// Catch exception if any
  28.         System.err.println("Error: " + e.getMessage());
  29.     }
  30.        
  31. private static double[] getDoubleArray(String strLine) {
  32.     double[] a;
  33.     String[] split = strLine.split("[,)]"); //split the line at the ',' and ')' characters
  34.     a = new double[split.length-1];
  35.     for(int i = 0; i < a.length; i++){
  36.         a[i] = Double.parseDouble(split[i+1]); //get the double value of the String
  37.     }
  38.     return a;
  39. }
  40.        
  41. Array[] first= line.split("("); //first[2] will contain the values
  42.        
  43. Array[] arrayList = first[2].split(",");