Hi, I have a text file that I'm going to read, and then parse it. After parsing it, how do I return it into it's original position or layout as before? Because now after parsing it, my output is printed out individually, instead of printing it out how it looks like originally. May I know how to do it?

My text file actually contains only numbers (decimal and has negative numbers too). I actually want to store the output after parsing into a 2D array, but it doesn't seem to work. I need some help here.

You could get a String and add one line at a time to it, instead of individual integers.

String str="",output="";
          try{
            BufferedReader in = new BufferedReader(new FileReader("input.txt"));
            
            while ( (str=in.readLine()) != null){

                output+=str;
            }
            in.close();
          
        }

After this, you could split the output string into individual characters (numbers) with the " " separator and put them into your 2d array.

Source: http://www.coderanch.com/t/386279/java/java/string-split-any-delimiter

Pattern p = Pattern.compile(myDelimiter, Pattern.LITERAL) ;
String[] result = p.split(myString);

Or, for maximum simplicity but loss of time, read twice from your file. One time number by number, into your 2d array, second time line by line into the string.

I don't quite get it. As for the first part, the output is actually empty right? It's not the output after I get from reading the file.

Which first part? did you try it ? the output string contains all the lines in the input.txt file.

Yes I know. But in your example, your variable output is actually empty right?

import java.io.*;

public class Testing3
{

	static double[][] mydouble = new double [1000][1000];
    static int a, b;
    static double d;
	static String[] temp;

	public static void main(String args[]) throws Exception
	{

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

            String str;
            while ((str = in.readLine()) != null)	//file reading
            {
				
				temp = str.split(",");


                for (String s : temp)
                {

                	d = Double.parseDouble(s);
					mydouble[a][b] = d;
					System.out.println(mydouble[a][b]);

                }

			//System.out.println(str);

            }
            in.close();


        }catch( IOException ioException ) {}

	}

}

This is my codes so far. The output of the codes is all individually printed, and not in the original format as the one in the text file.

Yes I know. But in your example, your variable output is actually empty right?

No, it's not, it contains all the codes in their original format. You need to add this to your code:

import java.io.*;

        ...

            String str, output="";   // add this 
            while ((str = in.readLine()) != null)	//file reading
            {
				output=output+str;     // add this
				temp = str.split(",");


                for (String s : temp)
                {

                	d = Double.parseDouble(s);
					mydouble[a][b] = d;
					System.out.println(mydouble[a][b]);

                }

			//System.out.println(str);

            }
            in.close();

        System.out.print(output);      // ADD this

        }catch( IOException ioException ) {}

	}

}

Oh I can only access the output of it outside the while loop? But when I do what you asked me to add, is the output already parsed? However it is not exactly printed the way the original one is.

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.