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

Reading file and store it into 2D array and parse it

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! ):

Attachments Testing2.txt (4.42KB)
Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Okay, what code have you got so far?

leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
 

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?

coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 
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.

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

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.
}
apines
Practically a Master Poster
633 posts since Apr 2007
Reputation Points: 129
Solved Threads: 55
 

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

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

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?

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

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

apines
Practically a Master Poster
633 posts since Apr 2007
Reputation Points: 129
Solved Threads: 55
 
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.

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

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

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


What you mean by word wrap?

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

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?

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

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

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

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

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

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

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

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?

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

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 :).

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

Okay thank you so much for your help! :D

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

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.

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

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();
coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 

This question has already been solved

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