Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-02-2010
Member
 
Join Date: Dec 2009
Posts: 292
Read file to create a matrix

Hello,
I want to read a file to create a two-dimensional array and store the size of this table. I created a class matrix. In another method constMat class.
I am getting any error in my code, this is my code, please have a look at it.
Code:
Public static Matrix ConstMat(String path){
			int t =0;
			
			int s =0;
BufferedReader file;
			try {
				file = new BufferedReader(new FileReader(path));
				try {
					String ln1;
ln1 = file.readln();
s = Integer.function valueOf() {
    [native code]
}(ln1);
Matrix m1 = new Matrix(s);
					while(file.read()!=-1){
						
						if(Turn! =0){
							int i =0;
String ln;
ln = file.readln();
StringTokenizer st = new StringTokenizer(row," ");
							while (st.hasMoreTokens()){
								int con;
con = Integer.function valueOf() {
    [native code]
}(st.nextToken());
m1.SetCell(con, t, i);
i + +;
							}
						t + +;
						}
						
					}
					
				} catch (NumberFormException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}   
			ret m1;
			
	}
Reply With Quote
  #2  
Old 25-02-2010
Member
 
Join Date: Nov 2009
Posts: 356
Re: java code for two dimensional array

Hello,
I have not clearly understood what exactly are you trying to do here. But my suggestion for you is
Code:
if(Turn! =0)
because then I do not see how he does not go on indefinitely complete turn = 0. If you need more information on array and two dimensional arrays and how to handle them in java then you can visit the sun's official site. Best of Luck
Reply With Quote
  #3  
Old 25-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Read file to create a matrix

Hello,
you must define your matrix before you are doing the rest. I mean to say, take a look at the example below
Code:
Public static Matrix ConstMat(String path){
			int con =0;
			int s =0;
BufferedReader file;
                        Matrix m1
You are doing this exactly reverse and that is why I think you are getting an error. If you fix it then I think the error will be removed and your code will compile successfully.
Reply With Quote
  #4  
Old 25-02-2010
Member
 
Join Date: Dec 2009
Posts: 292
Re: Read file to create two dimensional array

Hello,
This is my matrix class, please check and if you find any problem in the clas then please guide me with the correct one.
Code:
Public class Matrix {
	int tmp;
	int[][] mt;
	Public Matrix(int s){
		tmp = s;
mt =new int[s][s];
	}
	Public void SetCell(int val, int row, int Pass){
		mt[row][Pass]= val;
	}
}
Reply With Quote
  #5  
Old 25-02-2010
Member
 
Join Date: Nov 2009
Posts: 446
Re: Read file to create a matrix

Hello,
I think you can do it also with the ListIterator , though I am not sure of this because I have not tried it. Just have a look at the code and if you need then use it.
Code:
 ListIterator  li = list.listIterator ();
	  Object [] barr = new Object[i];
	  String [] str = new String[i];
 
	   while (li.hasNext()) {
	   barr[counter]=li.next();
	   str[counter]=barr[counter].toString();
 
           counter++;
        }
Reply With Quote
  #6  
Old 25-02-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Read file to create a matrix

Hello,
For creating a matrix you need to use the two dimensional array in your program and here is the example of the two dimensional array in java
Code:
int[][] t;
 t = new int[10][];  // allocate array of rows
 for (int r=0; r<t.length; r++) {
     t[r] = new int[r+1];
 }
 
 // print the tangular array (same as above really)
 for (int r=0; r<t.length; r++) {
     for (int c=0; c<t[r].length; c++) {
         System.out.print(" " + t[r][c]);
     }
     System.out.println("");
 }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Read file to create a matrix"
Thread Thread Starter Forum Replies Last Post
Need help to create cross Reference table/Matrix in Microsoft Excel Eas!war MS Office Support 2 15-02-2012 05:01 PM
New Matrix Movie (Matrix ReEntered) Vicious Off Topic Chat 8 08-02-2011 08:24 PM
cannot delete file -- cannot read from source file or disk Amitgujaran Windows XP Support 3 11-11-2010 09:55 PM
Any fastest way to compute 3x3 Matrix inverse and Matrix multiplication? Logan.B Software Development 4 25-09-2010 05:11 PM
XP: Cannot delete file: Cannot read from the source file or disk. iexplorer0726 Operating Systems 0 10-09-2007 05:07 AM


All times are GMT +5.5. The time now is 04:58 AM.