Results 1 to 16 of 16
  1. #1
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default Program can run but output all null

    Java Code:
    import java.util.ArrayList;
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName1 = "InputFile1.txt";
        String fileName2 = "InputFile2.txt";
        int i;
        String inLine1=null;
        String inLine2=null;
    
          // set up input stream1
        FileReader fr1 = new
            FileReader(fileName1);
          // buffer the input stream
        BufferedReader br1 =
            new BufferedReader(fr1);
    
          // set up input stream2
        FileReader fr2 = new
            FileReader(fileName2);
          // buffer the input stream
        BufferedReader br2 =
            new BufferedReader(fr2);
    
          // read and display1
        String buffer1 = "";
    
        ArrayList<String> File1 = new ArrayList<String>();
        while ((buffer1 = br1.readLine()) != null) {
        	File1.add(buffer1);
          System.out.println("File1 values: "+ inLine1);  // display the line
      }
      br1.close();
    
    
    
        //Now read the second file or make for this separate method
          // read and display2
        String buffer2 = "";
    
        ArrayList<String> File2 = new ArrayList<String>();
        while ((buffer2 = br2.readLine()) != null) {
        	File2.add(buffer2);
          System.out.println("File2 values: " + inLine2);  // display the line
      }
      br2.close();
    
        //Read all the lines in array or list
        //After that you can calculate them.
     }
    
    }
    Creating this simple program but get messed up previously.
    What is the mistake in this program? Correct me.
    Attached Files Attached Files

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    import java.io.*;
    
    public class ReadTextFilesRx
    {
        public static void main(String[] args) throws java.io.IOException
        {
            String fileName1 = "InputFile1.txt";
            String fileName2 = "InputFile2.txt";
            // These are not used below.
    //        int i;
    //        String inLine1=null;
    //        String inLine2=null;
    
            // set up input stream1
            FileReader fr1 = new FileReader(fileName1);
            // buffer the input stream
            BufferedReader br1 = new BufferedReader(fr1);
    
            // read and display1
            String buffer1 = "";
    
            List<String> File1 = new ArrayList<String>();
            while ((buffer1 = br1.readLine()) != null) {
                File1.add(buffer1);
    //            System.out.println("File1 values: "+ inLine1);
                System.out.println("File1 values: "+ buffer1);
            }
            br1.close();
    
    
    
            //Now read the second file or make for this separate method
            // read and display2
            // set up input stream2
            FileReader fr2 = new FileReader(fileName2);
            // buffer the input stream
            BufferedReader br2 = new BufferedReader(fr2);
    
            String buffer2 = "";
    
            List<String> File2 = new ArrayList<String>();
            while ((buffer2 = br2.readLine()) != null) {
        	    File2.add(buffer2);
    //            System.out.println("File2 values: " + inLine2);
                System.out.println("File2 values: " + buffer2);
            }
            br2.close();
    
            //Read all the lines in array or list
            System.out.println("File1 = " + File1);
            System.out.println("File2 = " + File2);
            //After that you can calculate them.
        }
    }

  3. #3
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Thanks hardwired for the useful guides.
    For newbie, I have difficulties in ArrayList<String> when dealing to parse the values for calculation.

    I wish to sum the values in "InputFile1.txt" and "InputFile1.txt" line by line to gives the output.
    Java Code:
    InputFile1   InputFile2	     TotalSum
    1.2		1.3		2.5
    0.1		9.1		9.2
    2.5		2.9		5.4
    4.6		0.6		5.2
    9.1		9.1		18.2
    10.3		10.8		21.1
    12.6		17.6		30.2
    2.2		22.2		24.4
    7.1		7.11		14.21
    4.51		3.51		8.02
    4.6		0.6		5.2
    19.1		9.1		28.2
    10.3		0.3		10.6
    0.0		0.01		0.01
    1.22		1.22		2.44
    -2.1		-2.1		-4.2
    9.5		9.51		19.01
    12		2		14
    43		4.3		47.3
    77.1		7.1		84.2
    0.5				0.5
    Where to create loop or while loop to parse those values?

    I created a simple for loop but not sure bout it,
    Java Code:
            for(int i=0;i<?;i++){
    			double total[] = new double[i];
    			double values1[] = new double[i];
    			double values2[] = new double[i];
    			values1[i] = Double.parseDouble(buffer1);
    			values2[i] = Double.parseDouble(buffer2);
    			total[i] = values1[i] + values2[i];
    		}
    The value "?" should be set to what so that it will be something likes buffer=""

    I got also got this error when I put the for loop after the line "br2.close();"
    Java Code:
    ReadTextFilesRx.java:68: reached end of file while parsing
    }→
     ^
    1 error
    How to create a useful 'for loop' for every list value and then parse it because need to get every total.
    Last edited by matt_well; 07-22-2008 at 01:18 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    ReadTextFilesRx.java:68: reached end of file while parsing
    Could you copy and paste the full text of the error message. Don't edit it any.
    You need to post your code for around the area where the error occurred- line 68

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I have difficulties in ArrayList<String> when dealing to parse the values for calculation
    Since you had ArrayLists to hold the input/read-in data I assumed that you wanted to read in all the data from the two files, close the files/readers and then process the data later.
    This is an elegant approach but there are other ways you can do it.
    The "<String>" part is a generic type parameter. Without these or "@Suppress" statements we get compiler warnings (which are unpleasant). See Lesson: Generics for more about generics.
    Java Code:
    for(int i=0;i<?;i++){
        double total[] = new double[i];
        double values1[] = new double[i];
        double values2[] = new double[i];
        values1[i] = Double.parseDouble(buffer1);
        values2[i] = Double.parseDouble(buffer2);
        total[i] = values1[i] + values2[i];
    }
    The value "?" should be set to what so that it will be something likes buffer=""
    This is handily solved with the ArrayList approach. Another option is to read the files once to get the number of lines, allocate the arrays, ie, with the number of lines in each file, and read them again to load the data into the arrays. Another way is to start with zero-length arrays and add an element on the fly as you read each file line. You can use System.arraycopy for this last way.
    I got also got this error when I put the for loop after the line "br2.close();"
    Code:
    Java Code:
    ReadTextFilesRx.java:68: reached end of file while parsing
    }→
     ^
    1 error
    Looks like you may have missed/misplaced a curley brace.
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class ReadTextFilesRx
    {
        public static void main(String[] args) throws java.io.IOException
        {
            String fileName1 = "InputFile1.txt";
            String fileName2 = "InputFile2.txt";
    
            // Read files and collect data for processing:
            List<String> file1Data = getData(fileName1);
            List<String> file2Data = getData(fileName2);
    
            // Let's see what we have in the lists:
            System.out.println("file1Data = " + file1Data);
            System.out.println("file2Data = " + file2Data);
    
            // Now it's easy to get the number of data items:
            System.out.println("file1Data size = " + file1Data.size());
            System.out.println("file2Data size = " + file2Data.size());
    
            //After that you can calculate them.
            System.out.printf("%s    %s    %s%n",
                              "InputFile1", "InputFile2", "Totals");
            int maxSize = Math.max(file1Data.size(), file2Data.size());
            for(int i = 0; i < maxSize; i++)
            {
                String data1 = getNextValue(file1Data, i);
                String data2 = getNextValue(file2Data, i);
                double one = parseValue(data1);
                double two = parseValue(data2);
                System.out.printf("%7s %12s %12.2f%n",
                                   data1, data2, one + two);
            }
        }
    
        private static List<String> getData(String filePath) throws IOException
        {
            List<String> list = new ArrayList<String>();
            FileReader fr = new FileReader(filePath);
            BufferedReader br = new BufferedReader(fr);
            String buffer;
            while( (buffer = br.readLine()) != null)
            {
                list.add(buffer);
            }
            br.close();
            return list;
        }
    
        private static String getNextValue(List<String> list, int index)
        {
            if(index > list.size()-1) // gone past end of list
                return "";
            return list.get(index);
        }
    
        private static double parseValue(String s)
        {
            if(s.equals("")) // missing data values appear as empty string
                return 0;
            return Double.parseDouble(s);
        }
    }

  6. #6
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Thanks hardwired. :)

    I tried to get the printed results into a textfile call "results.txt", I have created a FileWriter code and put it below at the main part after the line System.out.printf("%7s %12s %12.2f%n", data1, data2, sum);

    I get the sum become,
    Java Code:
    double sum = one + two;
    Java Code:
            	FileWriter fw = new FileWriter("C:\\results.txt");
    		BufferedWriter bw = new BufferedWriter(fw);
            	String Sum = Double.toString(sum);
            	bw.write(Sum);

    The program can be compiled with no errors but I get an empty "results.txt" file in my C drive after that.

    In future, if I wish to save the "C:\\results.txt" into the same directory as the program location, I should change the "C:\\" to what?


    In java, is there anyway to get the overall printed results into the "results.txt" directly?


    To make the results with index appear to be in the "results.txt" file be something like this below,
    Java Code:
    Index	InputFile1   InputFile2	     TotalSum
    0	1.2		1.3		2.5
    1	0.1		9.1		9.2
    2	2.5		2.9		5.4
    3	4.6		0.6		5.2
    4	9.1		9.1		18.2
    5	10.3		10.8		21.1
    6	12.6		17.6		30.2
    7	2.2		22.2		24.4
    8	7.1		7.11		14.21
    9	4.51		3.51		8.02
    10	4.6		0.6		5.2
    11	19.1		9.1		28.2
    12	10.3		0.3		10.6
    13	0.0		0.01		0.01
    14	1.22		1.22		2.44
    15	-2.1		-2.1		-4.2
    16	9.5		9.51		19.01
    17	12		2		14
    18	43		4.3		47.3
    19	77.1		7.1		84.2
    20	0.5				0.5
    Last edited by matt_well; 07-23-2008 at 08:29 AM.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    To save the file in the same location as the project you working on, change the following code,

    Java Code:
    FileWriter fw = new FileWriter("C:\\results.txt");
    as follows,

    Java Code:
    FileWriter fw = new FileWriter("results.txt");

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Try this in place of the last section in the main method above:
    Java Code:
            //After that you can calculate them and write to file.
            String path = "readTextFilesResults.txt";
            PrintWriter pw = new PrintWriter(
                             new FileOutputStream(path));
            pw.printf("%s    %s    %s%n",
                      "InputFile1", "InputFile2", "Totals");
            int maxSize = Math.max(file1Data.size(), file2Data.size());
            for(int i = 0; i < maxSize; i++)
            {
                String data1 = getNextValue(file1Data, i);
                String data2 = getNextValue(file2Data, i);
                double one = parseValue(data1);
                double two = parseValue(data2);
                pw.printf("%7s %12s %12.2f%n",
                           data1, data2, one + two);
            }
            pw.close();

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Craig, I want to know one thing, actually it is off topic with this thread, but not much valid to start a new.

    I've seen that in lots of such similar cases you use PrintWriter, rather FileWriter. Any reason for that?

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Actually, I usually use
    Java Code:
    new BufferedWriter(
    new OutputStreamWriter(
    new FileOutputStream(
    because it will write about anything. Same for the reader correlates.

    I generally do not use FileWriter because it is more limited/fragile. It is a convenience class for text. It's good to use for reading into JTextArea.

    Above I used PrintWriter because it matched up nicely with the System.out.printf statements in place. I couldn't find a BufferedWriter arrangement/wrapper for it.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I agreed with you mate. Normally I use FileWriter for text file manipulations in small size applications. To working with larger files I used one of them you mentioned above.

    I really used PrinWriter so far, because I'm not much familiar with it. Actually nothing much to worried about it.

    Anyway, thanks for the explanation. :)

  12. #12
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Ok, get it hardwired.
    How to make the last value of output at InputFile2 there to become "0" where in future any "" no value to become "0"?
    Java Code:
    Index	InputFile1   InputFile2	     TotalSum
    0	1.2		1.3		2.5
    1	0.1		9.1		9.2
    2	2.5		2.9		5.4
    3	4.6		0.6		5.2
    4	9.1		9.1		18.2
    5	10.3		10.8		21.1
    6	12.6		17.6		30.2
    7	2.2		22.2		24.4
    8	7.1		7.11		14.21
    9	4.51		3.51		8.02
    10	4.6		0.6		5.2
    11	19.1		9.1		28.2
    12	10.3		0.3		10.6
    13	0.0		0.01		0.01
    14	1.22		1.22		2.44
    15	-2.1		-2.1		-4.2
    16	9.5		9.51		19.01
    17	12		2		14
    18	43		4.3		47.3
    19	77.1		7.1		84.2
    20	0.5				0.5

    Make into this,
    Java Code:
    Index	InputFile1   InputFile2	     TotalSum
    0	1.2		1.3		2.5
    1	0.1		9.1		9.2
    2	2.5		2.9		5.4
    3	4.6		0.6		5.2
    4	9.1		9.1		18.2
    5	10.3		10.8		21.1
    6	12.6		17.6		30.2
    7	2.2		22.2		24.4
    8	7.1		7.11		14.21
    9	4.51		3.51		8.02
    10	4.6		0.6		5.2
    11	19.1		9.1		28.2
    12	10.3		0.3		10.6
    13	0.0		0.01		0.01
    14	1.22		1.22		2.44
    15	-2.1		-2.1		-4.2
    16	9.5		9.51		19.01
    17	12		2		14
    18	43		4.3		47.3
    19	77.1		7.1		84.2
    20	0.5		0		0.5

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    In getNextValue return"0" instead of "" when index is greater than the last index of the list.
    Since there are no blank/empty strings to deal with you can dispense with the parseValue method and parse the strings in the for loop.
    Java Code:
    double one = Double.parseDouble(data1);
    double two = Double.parseDouble(data2);

  14. #14
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Hi, something extra I wana ask.
    The two original files are as follow:
    InputFile1.txt
    Java Code:
    1.2
    0.1
    2.5
    4.6
    9.1
    10.3
    12.6
    2.2
    7.1
    4.51
    4.6
    19.1
    10.3
    0.0
    1.22
    -2.1
    9.5
    12
    43
    77.1
    0.5
    InputFile2.txt
    Java Code:
    1.3
    9.1
    2.9
    0.6
    9.1
    10.8
    17.6
    22.2
    7.11
    3.51
    0.6
    9.1
    0.3
    0.01
    1.22
    -2.1
    9.51
    2
    4.3
    7.1
    If the reading of InputFile2 is read with a "delay of three" steps index than InputFile1, which mean when reading of InputFile2 happen, there will be three "0" to read first before the reading continue. The InputFile2 is not modified. How to make it in this case?


    Imagination reading of InputFile2,
    Java Code:
    0
    0
    0
    1.3
    9.1
    2.9
    0.6
    9.1
    10.8
    17.6
    22.2
    7.11
    3.51
    0.6
    9.1
    0.3
    0.01
    1.22
    -2.1
    9.51
    2
    4.3
    7.1
    Last edited by matt_well; 07-23-2008 at 06:17 PM.

  15. #15
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    One way you could do this is to add the extra values to the list after reading the file.
    The ArrayList class api has a method we can use:
    Java Code:
    // Add 3 values to beginning of file2Data.
    for(int i = 0; i < 3; i++) {
        file2Data.add(0, "0");
    }

  16. #16
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Ok, this other thing, it is not delay of "0" at the input reading there but it is occur at the middle.

    In this case, for further calculation propose, is there anyway to create a method so that after the line "String data2 = getNextValue(file2Data, i);" there will be a method to delay the reading by addition of three "0" to read first before the reading continue and after that continue the calculation.

    Java Code:
            for(int i = 0; i < maxSize; i++)
            {
                String data1 = getNextValue(file1Data, i);
                String data2 = getNextValue(file2Data, i);
                double one = parseValue(data1);
                double two = parseValue(data2);
                pw.printf("%7s %12s %12.2f%n",
                           data1, data2, one + two);
            }

    The two input files "InputFile1" and "InputFile2" still remain unchange, just disturb by adding three "0" at the beginning of InputFile2 at the middle of the process before the calculation.


    The output file will become:
    Java Code:
    Index    InputFile1    InputFile2    Totals
      0        1.2            0         1.20
      1        0.1            0         0.10
      2        2.5            0         2.50
      3        4.6          1.3         5.90
      4        9.1          9.1        18.20
      5       10.3          2.9        13.20
      6       12.6          0.6        13.20
      7        2.2          9.1        11.30
      8        7.1         10.8        17.90
      9       4.51         17.6        22.11
     10        4.6         22.2        26.80
     11       19.1         7.11        26.21
     12       10.3         3.51        13.81
     13        0.0          0.6         0.60
     14       1.22          9.1        10.32
     15       -2.1          0.3        -1.80
     16        9.5         0.01         9.51
     17         12         1.22        13.22
     18         43         -2.1        40.90
     19       77.1         9.51        86.61
     20        0.5            2         2.50
     21          0          4.3         4.30
     22          0          7.1         7.10

Similar Threads

  1. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 11:14 PM
  2. Null value
    By Michael in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 07:25 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. What is NULL
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 05:55 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •