Hi all, I need to read a text file (I've attached it, you can take a look), and then store all the numbers in it into a 2D array. After which, I need to parse it. I need urgent help here! ):

Recommended Answers

All 49 Replies

Okay, what code have you got so far?

Member Avatar for coil

Without your code it's difficult to help you, but just looking at the way you're doing it, is there any reason why you're reading, storing it, parsing, and presumably storing the parsed values in some other array as opposed to parsing it immediately after reading?

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

public class Testing4
{
	public static void main(String args[])
	{
		double[][] matrix = new double[1000][1000];
		int x=0, y=0;

		try
        {
		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.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]);
	        	}
	        	y=y+1;

			}
			x=x+1;


        	in.close();


        }catch( IOException ioException ) {}
	}
}

This is the codes I have so far. But the output is not printed out in rows and columns.

It won't shows in rows and numbers because you are using System.out.println all the time:

for (String str : values)
{
   double str_double = Double.parseDouble(str);
   matrix[x][y]=str_double;
   System.out.print(matrix[x][y] + " ");
}

You need to print the columns without a new line, and each time you finish a line (exit the inner loop), print a new line as well. Notice that I have changed the locations where you increment x and y - inside the appropriate loops.

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.print(matrix[x][y] + " ");
      y=y+1; //you have inserted a value to the former y, need to increment
   }
   x=x+1; // finished the row, need to increment the row number
   System.out.println(""); // print a new row.
}

Omg it's correct already!!!!!!!! Thank you so much!!!!!!!!!

Hi, now my output is correct already, however, it is printing an extra empty row in between every two lines. How can I remove that empty line?

It doesn't do it for me, can you please post your code with the changes?

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

public class Testing4
{
	public static void main(String args[])
	{
		double[][] myDouble = new double[1000][1000];
		int x=0, y=0;

		try
        {
		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\Testing2.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);
	        		myDouble[x][y]=str_double;
	        		System.out.print(myDouble[x][y] + " ");
					y=y+1;
	        	}
	        	System.out.println("");
	        	x=x+1;


			}
        	in.close();


        }catch( IOException ioException ) {}
	}
}

Here you go.

It doesn't show that extra line for me - perhaps your are using word wrap and it simply wraps the line?

Word wrap means that when a line is full, it will continue automatically to a new line. Assuming that you are using windows, copy all of the output from the program into notepad. Then go to Format and make sure that Word Wrap is not checked - still see those empty lines?

It's on command prompt, and I can't copy the output.

Then redirect the output to a file (at the end of you command type "> fileName.txt")

I can't type anything too. ): The moment I try typing something the window closes.

I tried using netbeans to run, and with the output there, i could copy and paste it in notepad. It doesn't have the empty line anymore. So meaning it's done already?

As far as I can tell it is the word wrap in the command line that causes all those phantom lines. The lines with the minus signs are longer than the ones without, which can explain the fact that the new line appears every second line. You can always try a smaller input data file if you really want to :).

Okay thank you so much for your help! :D

Hi apines. I need your help. Now that I can print out the output I want, I realised when I want to use the 2D array myDouble out of the class, it is empty. Why is it like that? I need to use the 2D array with the stored values in another class.

Member Avatar for coil

1. This thread is solved, so if you have a major issue, please start a new one.

2. Could you please post your code? Assuming 'myDouble' is the name of your 2D array, it should look something like this:

public double[][] getMyDouble() {
   return myDouble;
}

Then, when you need to access it:

double[][] anotherArray=<class/variable name>.getMyDouble();
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 did the exact same thing. However, it is not working. The output printed out is empty. Meaning, my 2D array residuescores is empty.

I've make the codes shorter to this so it's easier to understand:

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 static void main(String args[])
	{

		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]);
	        	}
				//System.out.println("");
			}


        	in.close();


        }catch( IOException ioException ) {}

	}

}

class testing extends ArrayTest
{
	int i=0, j=0;

	double[][] residuescores = ArrayTest.myDouble;

	void array()
	{
		System.out.print(ArrayTest.myDouble[i][j]);
	}

}

When I print out myDouble, it's empty. And even when I put a testing line,
System.out.println("123");, it does not print out 123. It seems like there is smth wrong. How can I solve it?

The main method is ArrayTest and not Testing - the main is not inherited, testing is never called and nothing is printed. Before changing the structure take a look at the main method, you forgot to increment x and y when populating myDouble:

while ((line = in.readLine()) != null)
{
   String[] values = line.split(",");
   for (String str : values)
   {
      strDouble = Double.parseDouble(str);
      myDouble[x][y] = strDouble;
      ++y; //increment the columns!
   }
      ++x; //increment the rows, and start over the columns
      y = 0;
}

Now, regarding the structure of your code - one solution is to move your method from main to the constructor, so when an ArrayTest object is constructed, its myDouble will be populated:

public ArrayTest()
{
   try
   {
      BufferedReader in = new BufferedReader(new  FileReader("D:\\Downloads\\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;
             ++y; //increment the columns
         }
         ++x; //increment the rows
         y = 0; //start over the columns
       }
       in.close();
   }
   catch(IOException ioException) { }//always good to log this
}

Now, in order to test your method you can create a main method inside TestArray as well, such as:

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

Now you have no usage to the testing class. You can always change your code so it won't be in the constructor but static like you wanted in the first place - just remember that you can't use the main in order to do that. The main is not inherited, and you need to use different methods and test them and the rest of the code in the main.

I did that, but the output is only printing all 0.0 and it wont stop.

It's printing 1000^2=1,000,000 elements, it won't fit in your console screen, and it will take some time to stop. If you want to be sure, make another, smaller, file and make the matrix 10^10 - then see if it prints all the elements.

Tried it and it works for me, post your code please.

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;
				//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(" ");
   			}
		}

}

class testing extends ArrayTest
{

}

This is what I've done after your help. Am I on the right track?

What can I tell you - I copy pasted your code and it works with a slight index problems, which occur because you didn't reset the columns in each iteration:

while ((line = in.readLine()) != null)
{
   String[] values = line.split(",");
   for (String str : values)
   {
      strDouble = Double.parseDouble(str);
      myDouble[x][y] = strDouble;
      y=y+1;
   }
   x=x+1;
   y=0; // <----- you need to reset the columns index.
}

So I can't see why your code does not work...

Okay I did what you said, but it's still printing out all 0.0 and it will not stop. ):

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.