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

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 1.35 KB  |  hits: 20  |  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. reading text file into constructor array (java)
  2. textfile data
  3.  
  4. line1row1 line1row2 55
  5. line2row1 line2row2 44
  6. line3row1 line3row2 33
  7.        
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. class Data{
  12.     public Data(String entry1, String entry2, int entry3){}
  13. }  
  14. public class readData {
  15.     public static void main(String[] args) throws Exception{
  16.     BufferedReader inFile = new BufferedReader (new FileReader ("scores.txt"));
  17.  
  18.     Data entrydata[] = new Data[3]; //create new constructor array
  19.     for(int i = 0; i < entrydata.length; i++ ){
  20.             entrydata[i] = inFile.readLine();
  21.         }
  22.     }
  23. }
  24.        
  25. Data entrydata[] = new Data[3];
  26. entrydata [0] = new Data("line1row1 ", "line1row2 ", 55);
  27. entrydata [1] = new Data("line2row1 ", "line2row2 ", 44);
  28. entrydata [2] = new Data("line3row1 ", "line3row2 ", 33);
  29.        
  30. String[] tmp = inFile.readline().split( " " );
  31. entrydata[i] = new Data( tmp[0], tmp[1], Integer.parseInt( tmp[2] ) );
  32.        
  33. class Data{
  34.     String entry1, entry2;
  35.     int entry3;
  36.     public Data(String[] datas) throws NumberFormatException {
  37.         entry1 = datas[0];
  38.         entry2 = datas[1];
  39.         entry3 = Integer.parseInt(datas[2]);
  40.     }
  41. }
  42.     .
  43.     .
  44.     .
  45.     .
  46. Data entrydata[] = new Data[3]; //this is not a constructor
  47. for(int i = 0; i < entrydata.length; i++ ){
  48.     entrydata[i] = new Data(inFile.readLine().split(" ")); //this is the constructor
  49. }