Hi apines, I am so sorry, but could you look at this code instead? This is the main problem that I face. The previous one was just another re-make so that it's easier to understand.

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); }
}

Line 87 you have the same loop, and you are not changing the indexing. Please correct all the stuff that we have talked about in the other code first.

import java.io.*;
import java.util.*;

class ArrayTest
{
	static double[][] myDouble = new double[1000][1000];
	static int x=0, y=0;
	static double strDouble;

	public ArrayTest()
	{

		try
        {
        	BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.txt"));

			String line;
			while ((line = in.readLine()) != null)
			{

				String[] values = line.split(",");

	        	for (String str : values)
	        	{
	        		strDouble = Double.parseDouble(str);
					myDouble[x][y] = strDouble;
	        		System.out.print(myDouble[x][y]);
	        		y=y+1;
	        	}
	        	x=x+1;
	        	y=0;
				System.out.println("");
			}


        	in.close();


        }catch( IOException ioException ) {}

	}


	public static void main(String[] args)
		{
   			ArrayTest arr = new ArrayTest();
  			double[][] residuescores = arr.myDouble;
   			for(int i = 0; i < myDouble.length; ++i)
   			{
				for(int j = 0; j < myDouble[i].length; ++j)
				{
	  				System.out.print(ArrayTest.myDouble[i][j] + " ");
				}
				System.out.println(" ");
   			}
		}

}

Okay this is what I have for the previous code. The output is just printing 0.0 nonstop. I don't get what is wrong with it.

Use the file attached, myDouble to be a 3x3 matrix:

static double[][] myDouble = new double[3][3];

Remember to change the path in your code to the new file. Still the same?

Okay it can print now, but it is printing twice. Like two times of the same output.

Because you ask it to print in the constructor (lines 27, 32) and in the main method (lines 52, 54).

Oh. Okay. Then can we move on to the other code? The longer one?

Sure, please post the other code after you have made the changes as we made in this code. You have similar problems there as you had here. Fix them, and if it still doesn't work post the new code again.

Similar problems? But I thought the other one was because my 2Darray was empty? Hmmm, the current one we did, you said cos I print out the result twice. I don't get it how it applies to the other code. =/

import java.io.*;
import java.util.*;

class ArrayTest
{
	static double[][] myDouble = new double[1000][1000];
	static int x=0, y=0;
	static double strDouble;

	public ArrayTest()
	{

		try
        {
        	BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.txt"));

			String line;
			while ((line = in.readLine()) != null)
			{

				String[] values = line.split(",");

	        	for (String str : values)
	        	{
	        		strDouble = Double.parseDouble(str);
					myDouble[x][y] = strDouble;
	        		//System.out.print(myDouble[x][y]);
	        		y=y+1;
	        	}
	        	x=x+1;
	        	y=0;
				//System.out.println("");
			}


        	in.close();


        }catch( IOException ioException ) {}

	}


	public static void main(String[] args)
		{
   			ArrayTest arr = new ArrayTest();
  			double[][] residuescores = arr.myDouble;
   			for(int i = 0; i < myDouble.length; ++i)
   			{
				for(int j = 0; j < myDouble[i].length; ++j)
				{
	  				System.out.print(ArrayTest.myDouble[i][j] + " ");
				}
				System.out.println(" ");
   			}
		}

}

This is the changes I've made. I only print the result at the main loop. But the output is still a lot of 0.0. I don't know why. If I change the text file to the text file you attached earlier on, and I only print result in main, the result would be the same as the text file. But I don't why it doesn't work after I change back to my original text file.

For example, in line 87 in the new code you have the exact same loop as in the older code, and you are not changing the indices, just like you didn't do in the older code. You have changed it in the older code and now it works - and in your new code you didn't. Review the new code, fix the similar problems, and post it again fixed.

Edit: You have posted the requested code as I was writing this post :)

The number of cells in your matrix should be the same as the numbers of elements in the text file - every extra cell has its initial value to be a 0.0. If you take my text file and put the matrix to be 4x4, you will see extra 0.0.

Oh. No I don't want to fix the matrix. Because I will be reading other text files too. Meaning, the matrix will not be fix, and it will depend on the text file I'm reading. So how do I do that?

While inserting the elements to the matrix you can count which cells were used, and then print only them to the user.

Your matrix is 1000*1000, and you don't want to print all of the values, and you don't want to change the size of the matrix. My suggestion is to save the values of x and y that you use when inserting the values into the matrix, and use them when printing the values to the user - instead of printing all of the array, print only until the x and y.

i want to read text file into 2D array.My text File Contains Floating Number .can Anyone help me?i am new in java

Read this thread - it contains a complete discussion.
If you are still stuck start a new thread of your own, and post what you have done so far.
Nobody will help you until you show that you are willing to make some effort.

double[][] matrix = new double[1000][1000];
        int x=0, y=0;
        try
        {
        BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\lamichhane ba\\Desktop\\test.txt"));  //reading files in specified directory
            String line;
            while ((line = in.readLine()) != null)  //file reading
            {
                {
                String[] values = line.split(",");
                for (String str : values)
                {
                    double str_double = Double.parseDouble(str);
                    matrix[x][y]=str_double;
                    System.out.println("Matrix["+x+"]["+y+"]"+matrix[x][y]);
                    y=y+1;
                }
                y=0;
            }
            x=x+1;
            }
            System.out.println(matrix[0][2]);
            in.close();
        }catch( IOException ioException ) {}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.