1 package JSci.io; 2 3 import java.lang.Double ; 4 import java.io.*; 5 import java.util.Vector ; 6 7 import JSci.maths.*; 8 9 15 public final class TextReader extends InputStreamReader { 16 private final BufferedReader reader=new BufferedReader(this); 17 20 public TextReader(InputStream stream) { 21 super(stream); 22 } 23 28 public TextReader(String name) throws FileNotFoundException { 29 super(new FileInputStream(name)); 30 } 31 36 public TextReader(File file) throws FileNotFoundException { 37 super(new FileInputStream(file)); 38 } 39 43 public int read() throws IOException { 44 return reader.read(); 45 } 46 50 public double[][] readArray() throws IOException { 51 final Vector data=new Vector (10); 52 for(int ch=read();ch!='\n' && ch!=-1;) { 53 if(isNumber(ch)) { 54 StringBuffer str=new StringBuffer (); 55 do { 56 str.append((char)ch); 57 ch=read(); 58 } while(isNumber(ch)); 59 data.addElement(str.toString()); 60 } 61 while(!isNumber(ch) && ch!='\n' && ch!=-1) 62 ch=read(); 63 } 64 int cols=data.size(); 65 int rows=1; 66 for(int ch=read();ch!=-1;) { 67 if(isNumber(ch)) { 68 StringBuffer str=new StringBuffer (); 69 do { 70 str.append((char)ch); 71 ch=read(); 72 } while(isNumber(ch)); 73 data.addElement(str.toString()); 74 } 75 while(!isNumber(ch) && ch!='\n' && ch!=-1) 76 ch=read(); 77 if(ch=='\n') { 78 ch=read(); 79 rows++; 80 } 81 } 82 double array[][]=new double[rows][cols]; 83 for(int j,i=0;i<rows;i++) { 84 for(j=0;j<cols;j++) 85 array[i][j]=Double.parseDouble(data.elementAt(i*cols+j).toString()); 86 } 87 return array; 88 } 89 private boolean isNumber(int ch) { 90 return Character.isDigit((char)ch) || ch=='.' || ch=='+' || ch=='-' || ch=='e' || ch=='E'; 91 } 92 } 93 94 | Popular Tags |