954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Empty 2D array from another class

Hi all, I'm actually equating a 2D array from a class to another class. I call it using method call. However, it seems like the 2D array is empty after I equate it to another 2D array in another class. May I know where is the problem?

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
 
class MatchApplet extends JApplet {
 
 
	TextArea outputArea;
	JButton button;
	JButton reset;
 
	JTextField tF1;
	JTextField tF2;
 
	JLabel l1;
	JLabel l2;
 
	String s1;
	String s2;
 
	static String display1 = "";
  	static String display2 = "";
 
 
static double[][] myDouble = new double[1000][1000];
	static int x=0, y=0;
 
 
 
	public static void main(String[] args)
	{
            //Application for program
            Frame f = new Frame();
            f.addWindowListener(new java.awt.event.WindowAdapter()
            {
                public void windowClosing(java.awt.event.WindowEvent e)
                {
                System.exit(0);
                };
            });
 
  		MatchApplet ut = new MatchApplet();
  		ut.setSize(900,900); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(900,900 + 100); // add 20, seems enough for the Frame title,
  		f.show();
		//end of application for program
 
	}//end of public static void main(String[] args)
 
 
 
  	public void init()
	{
 
		try
		{
		BufferedReader in1 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a00(50).txt"));	//reading files in specified directory
		BufferedReader in2 = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\1a0a(50).txt"));	//reading files in specified directory
		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.txt"));	//reading files in specified directory
 
 
  			String str1;
			while ((str1 = in1.readLine()) != null)	//file reading
			{
				display1 = str1;
				System.out.print(display1);
  			}
  			in1.close();
 
  			System.out.println("");
 
			String str2;
			while ((str2 = in2.readLine()) != null)	//file reading
			{
				display2 = str2;
				System.out.print(display2);
  			}
			in2.close();
 
			System.out.println("");
 
			String line;
			while ((line = in.readLine()) != null)
			{
 
				String[] values = line.split(",");
 
	        	for (String str : values)
	        	{
	        		double str_double = Double.parseDouble(str);
	        		myDouble[x][y] = str_double;
	        		System.out.print(myDouble[x][y]);
	        	}
	        	System.out.println("");
			}
 
 
			in.close();
 
 
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
 
		outputArea = new TextArea(40,110);
		Font font = new Font("Courier", Font.PLAIN, 12);
		outputArea.setFont(font);
		outputArea.setEditable(false);
 
		tF1 = new JTextField(display1);
		tF2 = new JTextField(display2);
 
		l1 = new JLabel("Sequence 1:");
		l2 = new JLabel("Sequence 2:");
 
		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(outputArea);
 
 
	final Substitution sub = new Blosum50();
 
		s1 += tF1.getText();
		s2 += tF2.getText();
		Output out = new Output ()
		{
	  		public void print(String s)
	  		{ outputArea.append(s); }
 
	  		public void println(String s)
	  		{ outputArea.append(s); outputArea.append("\n"); }
 
	  		public void println()
	  		{ outputArea.append("\n"); }
		};
 
	outputArea.setText("");
      (new NW      (sub, 8,     s1, s2)).domatch(out, "GLOBAL ALIGNMENT");
      (new SW      (sub, 8,     s1, s2)).domatch(out, "LOCAL ALIGNMENT");
 
 
      }catch( IOException ioException ) {}
 
 
  }//end of init()
 
      static double[][] getMyDouble()
      { return myDouble; }
 
}//end of class
 
 
 
 
// The class of substitution (scoring) matrices
 
abstract class Substitution {
	public double[][] score;
 
	void buildscore(String residues, String residues2, double[][] residuescores)
	{
	// Allow lowercase and uppercase residues (ASCII code <= 127):
	score = new double[127][127];
	for (int i=0; i<residues.length(); i++)
    {
		char res1 = residues.charAt(i);
		for (int j=0; j<=i; j++)
		{
        char res2 = residues2.charAt(j);
        score[res1][res2] = score[res2][res1]
	  = score[res1][res2+32] = score[res2+32][res1]
	  = score[res1+32][res2] = score[res2][res1+32]
	  = score[res1+32][res2+32] = score[res2+32][res1+32]
	  = residuescores[i][j];
      	}
    }
  	}
 
	abstract public String getResidues();
	abstract public String getResidues2();
}
 
 
// The BLOSUM50 substitution matrix for amino acids (Durbin et al, p 16)
 
class Blosum50 extends Substitution
{
 
	private String residues = MatchApplet.display1;
	private String residues2 = MatchApplet.display2;
 
	public String getResidues()
	{ return residues; }
 
	public String getResidues2()
	{ return residues2; }
 
	private double[][] residuescores = MatchApplet.getMyDouble();
 
 
	public Blosum50()
	{ buildscore(residues, residues2, residuescores); }
}


I actually want the stored values in the 2D array in line 96, to be brought over to the 2D array in line 203. I used method call in line 152 and 153 to return the 2D array myDouble, and equate the method call to the 2D array in line 203. However, I realised my 2D array in line 96 is empty after it is out of the for loop. May I know how can I still store the values in it and call it elsewhere even when it is out of the for loop?

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Because you are using iterator but you never increase value of x and y you are using in the loop.

Insert y=0; before line 92
Insert y++; after line 96
Insert x++; before line 98

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

Ya okay that is done. But when I access my 2D array out of that loop it doesn't work anymore.

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

So did you print it out after the loop to see that the value exists? If yes, but when you call it from other class, what error you got?

If there is still a problem, you could try to create a copy of the array and return it.

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

I print it out of the loop, but it only print 0.0

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I tried making a copy of the array, but isn't it the same? Hmmm, it just can't seem to work.

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I have answered your same question here ...

apines
Practically a Master Poster
633 posts since Apr 2007
Reputation Points: 129
Solved Threads: 55
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: