	   //============================================
	   // Numerical Analysis Package in Java
	   // Class Text to read data from screen or file
	   // and dısplay data in matrix form
	   // Dr. Turhan Coban
	   //============================================
	   import java.io.*;
	   import java.util.*;
	   import javax.swing.*;
       import javax.swing.table.*;
       import java.awt.*;

            class Text
            {
            BufferedReader in;
	    /*
            ---------------------------------------------------
            Static routines provided are:
            ---------------------------------------------------
            public static void prompt(String s);
	        public static int readInt(BufferedReader in);
            public static double readDouble(BufferedReader  in);
            public static String readString(BufferedReader in);        
            public static char readChar(BufferedReader in);
	    ----------------------------------------------------
            Dynamic routines provided are :
            ----------------------------------------------------
            public void Text()
            public void Text(String s1)
            public void Text(File f1)
            public int readInt();
            public double readDouble();
            public String readString();        
            public char readChar();

            Sample use :
	    --------- reading a double--------------
           BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
            double number;
            number=Text.readDouble(cin);
            veya
            double number;
            Text cin=new Text();
            number=cin.readDouble();
	    ------ reading data from a file "datafile.dat"------
            double number;
            BufferedReader fin=new BufferedReader(new FileReader("datafile.dat"));
            double number;
            number=Text.readDouble(fin);
            veya
            double number;
            Text cin=new Text("dataFile.dat");
            number=cin.readDouble();
        ------ reading double array data from a file "datafile.dat"------ 
        double a[][]=Text.readDouble("datafile.dat");
        ------ reading double array data from screen ------ 
        double a[][]=Text.readDouble(3);
        double a[][]=Text.readDouble(3,4); 
        double a[][]=Text.readDouble_TextArea(3,4);
        double a[][]=Text.readDouble_TextArea();
        double a[][]=Text.readDouble_Table(3,4);
        double a[][]=Text.readDouble_Table(); 
        ------ displaying array data from screen ------ 
        double a[][]=new double{{1,2,3},{4,5,6},{7,8,9}};
        String heading={"column 1","column 2",column 3"};
        Text.print(a);
        Text.print(a'heading);
        Text.print("datafile.dat",heading);
        Text.print("datafile.dat");
        double a[][]=Text.readDouble(3,4); 
               
	    ------ printing data into a file "printfile.dat"----
	    doub
	    PrintStream fout=new PrintStream
	    (new FileOutputStream("printfile.dat"));
	    String a="turhan";
	    fout.println(a);
        veya
            BufferedWriter fout=new BufferedWriter(new FileWriter("datafile.dat"));
            String a="Turhan");
            fout.println(a);
	    ----------------------------------------------------
	    */
	    private static StringTokenizer T;
	    private static String S;

            public Text() 
            {
            in=new BufferedReader(new InputStreamReader(System.in));
            }

            public Text(String s1)  throws IOException
            {
            in=new BufferedReader(new FileReader(s1));
            }

            public static void prompt (String s) {
	        System.out.print(s + " ");
	        System.out.flush();
	       }

            public int readInt() throws IOException
            {return Text.readInt(in);}

            public double readDouble() throws IOException
            {return Text.readDouble(in);}

            public String readString() throws IOException
            {return Text.readString(in);}

            public char readChar() throws IOException
            {return Text.readChar(in);}

        public static int readInt (BufferedReader in) throws IOException
	    {
	      if (T==null) refresh(in);
	      while (true)
	      {
		try {
		String item = T.nextToken();
		return Integer.valueOf(item.trim()).intValue();
		     }
		catch (NoSuchElementException e1) { refresh (in);
	      } catch(NumberFormatException e2)
                { //System.err.println("Error in number, try again.");
                }
	      }
	     }
             
        public static char readChar (BufferedReader in) throws IOException
	    {
		if (T==null) refresh(in);
		while (true)
		{
		    try {
		     return T.nextToken().charAt(0);
			}
		     catch(NoSuchElementException e1) {refresh (in);}
			}
	    }

        public static double readDouble(BufferedReader in) throws IOException
	    {
		if(T==null) refresh(in);
		while (true) {
		      try {
		       String item = T.nextToken();
		      return Double.valueOf (item.trim()).doubleValue();
			  } catch(NoSuchElementException e1) {
			    refresh (in);
			  } catch(NumberFormatException e2)
                          {
                           //System.err.println("Error in number, try again.");
			  }
		}
	    }

       public static String readString(BufferedReader in) throws IOException
	    {
		if(T==null) refresh (in);
		while (true) {
		    try {return T.nextToken();}
		      catch (NoSuchElementException e1) {
		       refresh (in);
		      }
		}
	    }
      
      public static String readStringLine(BufferedReader in) throws IOException
      {
       //reads a line of strings from BufferedReader in
      int ch;
      String r = "";
      boolean done = false;
      while (!done)
      {  try
	     {  ch = in.read();
	     if(ch < 0 || (char)ch == '\n' || (char)ch == '\0') done = true;
	     else r = r + (char) ch;
	     }
	     catch(java.io.IOException e) { done = true;}
      }
      return r.substring(0,(r.length()-1));
      }
      
      public static String readStringLine(String s)
      {
       //reads a line of strings from String s
      int ch;
      String r = "";
      boolean done = false;
      int i=0;
      while (!done)
      {  
	     ch = s.charAt(i);i++;
	     if(ch < 0 || (char)ch == '\n' || (char)ch == '\0') done = true;
	     else r = r + (char) ch;
      }
      return r.substring(0,r.length());
      }

      private static void refresh (BufferedReader in) throws IOException
      {
      String s = in.readLine();
      if (s==null) throw new EOFException();
      T = new StringTokenizer(s);
      }
      
      public static double[][] readDoubleS(String s)
      {
       //read data in matrix form
        // data length is not limited
        Vector xi=new Vector(); 
        if(s.charAt(s.length())!='\n') s+="\n";
        int nn;
        int m=2;
        int i=0;      
        String s1=Text.readStringLine(s);
        s=s.substring(s1.length()+1,s.length());
	    StringTokenizer token=new StringTokenizer(s1);
        int n=token.countTokens()-1;
        m=n+1;        //m tokens boyutludeğişken kolon sayısı  
        while(token.hasMoreTokens())
        {
        String s2=token.nextToken();
        xi.add(s2);
        }  
        i=0;            
        while(s.length()!=0)
        {
        i++;
	    String s11=Text.readStringLine(s);
	    s=s.substring(s11.length()+1,s.length());	         
        if(s11=="") break;
	    StringTokenizer token1=new StringTokenizer(s11);       
        while(token1.hasMoreTokens())
        {
        String s2=token1.nextToken();
        xi.add(s2);
        }
        //if(s.length()==0) {break;}
        }
        nn=i+1;
        double aa[][]=new double[nn][m];
        Enumeration nx=xi.elements();
        for(i=0;i<nn;i++)
        {
        for(int j=0;j<m;j++)
        {
	    String ss1=(String)nx.nextElement();    
	    Double ax=new Double(ss1);
        aa[i][j]=ax.doubleValue();  
        }
      } 
        return aa;	  
	  }
	  
	  public static double[][] readDoubleS(String s,int n,int m)
      {
       //read data in matrix form
        // data length is not limited
        Vector xi=new Vector(); 
        if(s.charAt(s.length()-1)!='\n') s+="\n";
        int i,j;
        for(i=0;i<n;i++)
        {
	    String s11=Text.readStringLine(s);
	    s=s.substring(s11.length()+1,s.length());	         
        if(s11=="") break;
	    StringTokenizer token1=new StringTokenizer(s11);       
        for(j=0;j<m;j++)
        {
        String s2=token1.nextToken();
        xi.add(s2);
        }
        //if(s.length()==0) {break;}
        }
        double aa[][]=new double[n][m];
        Enumeration nx=xi.elements();
        for(i=0;i<n;i++)
        {
        for(j=0;j<m;j++)
        {
	    String ss1=(String)nx.nextElement();    
	    Double ax=new Double(ss1);
        aa[i][j]=ax.doubleValue();  
        }
      } 
        return aa;	  
	  }
	        
      public static double[][] readDouble(String filename)
      {
        //read data in matrix form
        // data length is not limited
        Vector xi=new Vector(); 
        int nn;
        int m=2;
        int i=0;
        BufferedReader fin;
        try{
	    fin=new BufferedReader(new FileReader(filename));
	    try {
		try{ 
		try{            	    
	    String s1=Text.readStringLine(fin);        
	    StringTokenizer token=new StringTokenizer(s1);
        int n=token.countTokens()-1;
        m=n+1;        //m tokens boyutludeğişken kolon sayısı  
        while(token.hasMoreTokens())
        {
        String s2=token.nextToken();
        xi.add(s2);
        }
        i=0;              
        while(fin != null)
        {
        i++;
	    String s11=Text.readStringLine(fin);
	    //System.out.println(s11);        
        if(s11=="") break;
	    StringTokenizer token1=new StringTokenizer(s11);       
        while(token1.hasMoreTokens())
        {
        String s2=token1.nextToken();
        xi.add(s2);
        }
        }
        } catch(ArrayIndexOutOfBoundsException e_eof) { }
        } catch(StringIndexOutOfBoundsException e_eof) { }
        } catch(EOFException e_eof) {System.out.println("end of file"); }
        } catch(IOException e_io) {System.out.println("file name is not existed"); }      
        nn=i;
        //System.out.println("m="+m+"nn="+nn);
        double aa[][]=new double[nn][m];
        Enumeration nx=xi.elements();
        for(i=0;i<nn;i++)
        {
        for(int j=0;j<m;j++)
        {
	    String s1=(String)nx.nextElement();    
	    Double ax=new Double(s1);
        aa[i][j]=ax.doubleValue();  
        }
      } 
        return aa;
     }
     
     public static double[][] readDoubleT(String filename)
     {//veriyi ters matris olarak okur(satir ve sutun degisir)
	 double a[][]=readDouble(filename);
     int n=a.length;
     int m=a[0].length;
     double b[][]=new double[m][n];
        for(int i=0;i<n;i++)
        {
        for(int j=0;j<m;j++)
        {
        b[j][i]=a[i][j];  
        }
      } 
     return b;
     }

     public static double[][] readDouble(int n,int m)
     {
     String s="line 0 ";    
	 String s1=JOptionPane.showInputDialog(s);        
     StringTokenizer token=new StringTokenizer(s1);
     double a[][]=new double[n][m];
     int j=0;           
     for(j=0;j<m;j++)
     {Double ax=new Double(token.nextToken());
     a[0][j]=ax.doubleValue();}
     for(int i=1;i<n;i++)
     {
	   s="line "+i;
	   s1=JOptionPane.showInputDialog(s);        
       token=new StringTokenizer(s1);
       j=0;           
       for(j=0;j<m;j++)
       {Double ax=new Double(token.nextToken());
       a[i][j]=ax.doubleValue();}
	 }
     return a; 
	 } 
            
     public static double[][] readDouble(int n)
     {
	 // column number determined by number of data entered in first column    
	 String s="line 0 ";    
	 String s1=JOptionPane.showInputDialog(s);        
     StringTokenizer token=new StringTokenizer(s1);
     int nn=token.countTokens()-1;
     int m=nn+1;
     double a[][]=new double[n][m];
     int j=0;           
     while(token.hasMoreTokens())
     {
     Double ax=new Double(token.nextToken());
     a[0][j++]=ax.doubleValue();
     }
     for(int i=1;i<n;i++)
     {
	   s="line "+i;
	   s1=JOptionPane.showInputDialog(s);        
       token=new StringTokenizer(s1);       
       for(j=0;j<m;j++)
       {Double ax=new Double(token.nextToken());
       a[i][j]=ax.doubleValue();}
	 }
     return a; 
     } 
     
      public static double[][] readDouble_TextArea(int n,int m) 
      {
	  girdi g=new girdi(n,m);
	  double a[][]=g.vericiktisi();
	  return a;
	  } 
	  
	  public static double[][] readDouble_TextArea() 
      {    
	  String s1=JOptionPane.showInputDialog("n(satır) ve m(sütun) boyutlarını veriniz :" );        
      StringTokenizer token=new StringTokenizer(s1);
      Integer ax=new Integer(token.nextToken());
      int n=ax.intValue();
      ax=new Integer(token.nextToken());
      int m=ax.intValue();     
	  girdi g=new girdi(n,m);
	  double a[][]=g.vericiktisi();
	  return a;
	  }
	   
	  public static double[][] readDouble_Table(int n,int m) 
      {
	  girdi1 g=new girdi1(n,m);
	  double a[][]=g.vericiktisi();
	  return a;
	  }
	   
	  public static double[][] readDouble_Table() 
      {
	  String s1=JOptionPane.showInputDialog("n(satır) ve m(sütun) boyutlarını veriniz :" );        
      StringTokenizer token=new StringTokenizer(s1);
      Integer ax=new Integer(token.nextToken());
      int n=ax.intValue();
      ax=new Integer(token.nextToken());
      int m=ax.intValue();    
	  girdi1 g=new girdi1(n,m);
	  double a[][]=g.vericiktisi();
	  return a;
	  } 
     
     public static double[][] T(double a[][])
     {
	 //Ters matris    
	 int n=a.length;
	 int i,j;
	 int m=a[0].length;
	 double b[][]=new double[m][n];    
     for(i=0;i<n;i++)
     { for(j=0;j<m;j++)
       {b[j][i]=a[i][j];}}
     return b;
     }
          
     public static void print(double a[][],String baslik[])
     {print(a,baslik,"data output(veri çıktısı) :");}
     
     public static void print(double b[],String baslik[],String bb)
     {
     double a[][]=new double[1][b.length];
     a[0]=b;
     print(a,baslik,bb);
	 }
	  
	 public static void print(double b[],String bb)
     {
     double a[][]=new double[1][b.length];
     a[0]=b;
     print(a,bb);
	 }
	 
	 public static void print(double b[])
     {
     double a[][]=new double[1][b.length];
     a[0]=b;
     print(a,"vektör çıktısı : ");
	 }
	  
	 public static void printT(double b[],String bb)
     {
     double a[][]=new double[b.length][1];
     for(int j=0;j<b.length;j++) a[j][0]=b[j];
     print(a,bb);
	 }    
     public static void printT(double b[])
     {
     double a[][]=new double[b.length][1];
     for(int j=0;j<b.length;j++) a[j][0]=b[j];
     print(a,"vektör çıktısı");
	 }             
     public static void print(double a[][],String baslik[],String bb)
     {
	 JTable jt;
	 int n=a.length;
	 int m=a[0].length;
	 genelModel gm=new genelModel(a,baslik);
	 jt=new JTable(gm);
	  //JOptionPane.showMessageDialog(null,jt);
     JFrame cerceve=new JFrame(bb);
     cerceve.addWindowListener(new BasicWindowMonitor());
     cerceve.getContentPane().add(new JScrollPane(jt));
     cerceve.pack();
     cerceve.setVisible(true);
	 }
	 	      
     public static void print(double a[][],String baslik)
     {
	 JTable jt;
	 int n=a.length;
	 int m=a[0].length;
	 genelModel gm=new genelModel(a);
	 jt=new JTable(gm);
	  //JOptionPane.showMessageDialog(null,jt);
     JFrame cerceve=new JFrame(baslik);
     cerceve.addWindowListener(new BasicWindowMonitor());
     cerceve.getContentPane().add(new JScrollPane(jt));
     cerceve.pack();
     cerceve.setVisible(true);
	 }
	 
	public static void print(double a[][])
	{print(a,"data output(veri çıktısı) :");}
	 
	 public static void print(String dosyaadi,String genelbaslik)
     {
	 genelModel gm=new genelModel(dosyaadi);
	 JTable jt=new JTable(gm);
	  //JOptionPane.showMessageDialog(null,jt);
     JFrame cerceve=new JFrame(genelbaslik);
     cerceve.addWindowListener(new BasicWindowMonitor());
     cerceve.getContentPane().add(new JScrollPane(jt));
     cerceve.pack();
     cerceve.setVisible(true);
     }
     	
	 public static void print(String dosyaadi,String[] baslik,String genelbaslik)
     {
	 genelModel gm=new genelModel(dosyaadi,baslik);
	 JTable jt=new JTable(gm);
	  //JOptionPane.showMessageDialog(null,jt);
     JFrame cerceve=new JFrame(genelbaslik);
     cerceve.addWindowListener(new BasicWindowMonitor());
     cerceve.getContentPane().add(new JScrollPane(jt));
     cerceve.pack();
     cerceve.setVisible(true);
	 }
	 	  
	 public static void print(String dosyaadi)
     {print(dosyaadi," "+dosyaadi+" :");}
     
     //==========
     //JComponent  added versions of print statements
     // main program should include a JPanel definition
     public static void print(JComponent anapencere,double a[][],String baslik[])
     {add(anapencere,a,baslik,"data output(veri çıktısı) :");}
           
     public static void add(JComponent anapencere,double a[][],String baslik[],String bb)
     {
	 JButton B;
	 B=new JButton(bb);    
	 JTable jt;
	 int n=a.length;
	 int m=a[0].length;
	 genelModel gm=new genelModel(a,baslik);
	 jt=new JTable(gm);
	 JScrollPane sp=new JScrollPane(jt); 
	 JPanel cerceve=new JPanel();
	 cerceve.setLayout(new BorderLayout());
	 cerceve.add(B,BorderLayout.NORTH);
	 cerceve.add(sp,BorderLayout.SOUTH);
	 anapencere.add(cerceve);
     }
	 
     public static void add(JComponent anapencere,double b[],String baslik[],String bb)
     {
	 double a[][]=new double[1][b.length];
     a[0]=b;
     add(anapencere,a,baslik,bb);
     }
     
     public static void addT(JComponent anapencere,double b[],String bb)
     { double a[][]=new double[b.length][1];
     for(int j=0;j<b.length;j++) a[j][0]=b[j];
     add(anapencere,a,bb);}
     
     public static void add(JComponent anapencere,double b[],String bb)
     {
	 double a[][]=new double[1][b.length];
     a[0]=b;
     add(anapencere,a,bb);
     }
     
     public static void addT(JComponent anapencere,double b[])
     {addT(anapencere,b,"vektör :");}
     
     public static void add(JComponent anapencere,double b[])
     {
	 double a[][]=new double[1][b.length];
     a[0]=b;
     add(anapencere,a,"vektör çıktısı");
     }
              
     public static void add(JComponent anapencere,double a[][],String baslik)
     {
	 JButton B;
	 B=new JButton(baslik);      
	 JTable jt;
	 int n=a.length;
	 int m=a[0].length;
	 genelModel gm=new genelModel(a);
	 jt=new JTable(gm);
	 JScrollPane sp=new JScrollPane(jt); 
	 JPanel cerceve=new JPanel();
	 cerceve.setLayout(new BorderLayout());
	 cerceve.add(B,BorderLayout.NORTH);
	 cerceve.add(sp,BorderLayout.SOUTH);
	 anapencere.add(cerceve);
	 }
	 
	public static void add(JComponent anapencere,double a[][])
	{add(anapencere,a,"data output(veri çıktısı) :");} 
	
	 public static void add(JComponent anapencere,String dosyaadi,String baslik)
     {
	 JButton B;
	 B=new JButton(baslik);       
	 genelModel gm=new genelModel(dosyaadi);
	 JTable jt=new JTable(gm);
	 JScrollPane sp=new JScrollPane(jt); 
	 JPanel cerceve=new JPanel();
	 cerceve.setLayout(new BorderLayout());
	 cerceve.add(B,BorderLayout.NORTH);
	 cerceve.add(sp,BorderLayout.SOUTH);
	 anapencere.add(cerceve);
	 }
	 
	 public static void print(JComponent anapencere,String genelbaslik)
	 {
	 Container c;
	 JFrame cerceve=new JFrame(genelbaslik);
	 cerceve.addWindowListener(new BasicWindowMonitor());
	 c=cerceve.getContentPane(); 
	 c.add(new JScrollPane(anapencere));
     cerceve.pack();
     cerceve.setVisible(true);	
     }
 	 	  
	 public static void add(JComponent anapencere,String dosyaadi)
     {add(anapencere,dosyaadi,""+dosyaadi+" :");}
     
     public static void printf(String dosyaadi,String s) throws IOException
     {  BufferedWriter fout=new BufferedWriter(new FileWriter(dosyaadi));            
        fout.write(s,0,s.length());
        fout.close();}
        
     public static void printf(String dosyaadi,double a) throws IOException
     {  String s=""+a;
	    BufferedWriter fout=new BufferedWriter(new FileWriter(dosyaadi));            
        fout.write(s,0,s.length());
        fout.close();}
      
  } 
                  

































