Page 1 of 2 12 LastLast
Results 1 to 20 of 37
  1. #1
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Smile Reading two text file and sum them up

    Hi, going to learn some Java for fun,
    I just wanna know how to make a program that can read two text files("InputFile1.txt" and "InputFile2.txt"), sum up the value line by line from both files and saved at the output file name "sum.txt"

    InputFile1.txt
    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
    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
    I have simply created two text files InputFile1.txt have 21 values and InputFile2.txt have 20 values.
    Add them up line by line from both file and just assume the 21st value at InputFile2.txt is zero.

    The ouput at sum.txt:
    2.5
    9.2
    5.4
    5.2
    18.2
    21.1
    30.2
    24.4
    14.21
    8.02
    5.2
    28.2
    10.6
    0.01
    2.44
    -4.2
    19.01
    14
    47.3
    84.2
    0.5
    Any guides from expert would be appreciate as I never seen a Java program that can read two txt files.
    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I guess you'd write a program that would read lines from the two input files, convert the data being read on each line into a double, add the two numbers together and write the output to the third file.
    So research how to read a file and how to write a file. There are lots of examples of code you could search for. Look for BufferedReader and BufferedWriter for example. Study those programs and see if you can then write your own.
    Good luck.

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Yes Norm is absolutely rite.
    You can read the values from file and store them in "array" of type double perform the addition and then write it back to another txt file.

    The basic of learning them how to do is left to you. If stuck up you are welcome back...
    To finish sooner, take your own time....
    Nivedithaaaa

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

    Default

    Best choice should be the ArrayList, since it's not unordered no need to worried about data retrieving.

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

    Default

    Thanks for those tips. I was in the process of learning Java, here I make a simple program. It however can only read one line, how should I do in order to read all the value from InputFile1.txt? And how to make it to read two textfiles at the same time? Correct me.

    Java Code:
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName = "InputFile1.txt";
        int i;
        String inLine;
    
          // set up input stream
        FileReader fr = new
            FileReader(fileName);
          // buffer the input stream
        BufferedReader br =
            new BufferedReader(fr);
    
          // read and display
    
          inLine = br.readLine();  // read a line
          System.out.println(inLine);  // display the line
    
        br.close();
     }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Look at how to use loops in a program. There are two primary statements: while and for. Read how to use these statements to repeat a task like reading lines from a file. Also Search for 'for' and 'while' to see sample code.

    To read 2 files at the same time, create a second FileReader/BufferedReader, say fr2 and br2

  7. #7
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by matt_well View Post
    Thanks for those tips. I was in the process of learning Java, here I make a simple program. It however can only read one line, how should I do in order to read all the value from InputFile1.txt? And how to make it to read two textfiles at the same time? Correct me.

    Java Code:
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName = "InputFile1.txt";
        int i;
        String inLine;
    
          // set up input stream
        FileReader fr = new
            FileReader(fileName);
          // buffer the input stream
        BufferedReader br =
            new BufferedReader(fr);
    
          // read and display
    
          inLine = br.readLine();  // read a line
          System.out.println(inLine);  // display the line
    
        br.close();
     }
    }
    K i give you tip how to do that
    Java Code:
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName = "InputFile1.txt";
        int i;
        String inLine;
    
          // set up input stream
        FileReader fr = new
            FileReader(fileName);
          // buffer the input stream
        BufferedReader br =
            new BufferedReader(fr);
    
          // read and display
        String buffer = "";
        ArrayList<String> firstFile = new ArrayList<String>();
        while ((buffer = br.readLine()) != null) {
        	firstFile.add(buffer);
        }
        //Now read the second file or make for this separate method
        //Read all the lines in array or list
        //After that you can calculate them.
        
        br.close();
     }
    }

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

    Default

    To read 2 files at the same time, create a second FileReader/BufferedReader, say fr2 and br2
    Is it to repeat right?

    Currently I am at my first learning on the input file reader and buffer reader stuff.
    Anyway to do it using for loop for reading the values, my code can only read one line of value.

    Can I know what is the meaning of these two lines code?
    Java Code:
    String buffer = "";
    Java Code:
        ArrayList<String> firstFile = new ArrayList<String>();

  9. #9
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Actually if you want read them at the same time you need to use threads at the movement it reads first file and then second.

    To read two files i would make method for that with filename parameter, which returns String array.

    String buffer = ""; this means you initialize as empty

    ArrayList<String> firstFile = new ArrayList<String>(); - this is litlle more advanced but this mostly same as array but its dynamical so you dont need to know the size, in the current code i dont know the size of lines in file, so its best use of it. <String> is generic so we dont need casting and it only stores String type data.

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

    Default

    Java Code:
    ReadTextFiles.java:20: cannot find symbol
    symbol  : class ArrayList
    location: class ReadTextFiles
        ArrayList<String> firstFile = new ArrayList<String>();
        ^
    ReadTextFiles.java:20: cannot find symbol
    symbol  : class ArrayList
    location: class ReadTextFiles
        ArrayList<String> firstFile = new ArrayList<String>();
                                          ^
    2 errors
    The tips of your code I used to compile are having two errors which I cant figure out the error problems, newbie needs guide here.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Perhaps save the suggests by jurka until you have the basics down. Save Threads and ArrayList<String> until you can write a simple program to do a simple task. Then learn how to improve it by use of the above.

    Here's a sample of what I mean by reading two files at a time:
    br1 and br2 are BufferedReaders set up to read from two files just as you defined br in your code above.

    Java Code:
      String recFromFile1 = br1.readLine();  // get the next line from file 1
      String recFromFile2 = br2.readLine();  // get the next line from file 2
    The compile errors you got were because the compiler could not find a definition for ArrayList.
    To use a class that is in a package, you need to tell the compiler where the definition for the class is.
    That is done by using an import statement. Read the API doc for the class that you want to use and see what package it is in. For ArrayList you should see:
    java.util
    Class ArrayList<E>
    at the top of the doc page for ArrayList. To use that package, add:
    import java.util.ArrayList;
    to your program.
    Last edited by Norm; 07-18-2008 at 08:55 PM.

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

    Default

    Java Code:
    import java.util.ArrayList;
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName = "InputFile1.txt";
        int i;
        String inLine;
    
          // set up input stream
        FileReader fr = new
            FileReader(fileName);
          // buffer the input stream
        BufferedReader br =
            new BufferedReader(fr);
    
          // read and display
        String buffer = "";
        ArrayList<String> firstFile = new ArrayList<String>();
        while ((buffer = br.readLine()) != null) {
        	firstFile.add(buffer);
          inLine = br.readLine();  // read a line
          System.out.println(inLine);  // display the line
      }
        //Now read the second file or make for this separate method
        //Read all the lines in array or list
        //After that you can calculate them.
    
        br.close();
     }
    
    }
    I have included import java.util.ArrayList; at the top to add package. The program can run now, but I get these at the output. It didn't show up all the 21 values in my "InputFile1.txt"? It shows something as null? Why?

    Java Code:
    0.1
    4.6
    10.3
    2.2
    4.51
    19.1
    0.0
    -2.1
    12
    77.1
    null

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    while ((buffer = br.readLine()) != null) {
    firstFile.add(buffer);
    inLine = br.readLine(); // read a line
    System.out.println(inLine); // display the line
    You are reading 2 times from br but only testing the one in the while() statement for being null.
    It appears the the code reads a line and saves it in firstFile and then reads a second line and prints it and then goes back to read the next line and save it. It seems you are saving every other line.??? You need to rethink what you want to do in this loop.

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

    Default

    Quote Originally Posted by Norm View Post
    You are reading 2 times from br but only testing the one in the while() statement for being null.
    It appears the the code reads a line and saves it in firstFile and then reads a second line and prints it and then goes back to read the next line and save it. It seems you are saving every other line.??? You need to rethink what you want to do in this loop.
    Ok, I done it and get the results. Thanks.
    Java Code:
        ArrayList<String> firstFile = new ArrayList<String>();
        while ((buffer = br.readLine()) != null) {
        	firstFile.add(buffer);
          System.out.println(buffer);  // display the line
      }
    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

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

    Default

    From this created program that can run,
    Java Code:
    import java.util.ArrayList;
    import java.io.*;
    public class ReadTextFiles
    {
      public static void main(String[] args)
        throws java.io.IOException
      {
        String fileName = "InputFile1.txt";
        int i;
        String inLine;
    
          // set up input stream
        FileReader fr = new
            FileReader(fileName);
          // buffer the input stream
        BufferedReader br =
            new BufferedReader(fr);
    
          // read and display
        String buffer = "";
        ArrayList<String> firstFile = new ArrayList<String>();
        while ((buffer = br.readLine()) != null) {
        	firstFile.add(buffer);
    
          System.out.println(buffer);  // display the line
      }
        //Now read the second file or make for this separate method
        //Read all the lines in array or list
        //After that you can calculate them.
    
        br.close();
     }
    
    }

    I now changed make it so that can read two textfiles before the next step to sum up the values in both text files line by line.
    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, inLine2;
    
          // 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> firstFile1 = new ArrayList<String>();
        while ((buffer1 = br1.readLine()) != null) {
        	firstFile1.add(buffer1);
          System.out.println(inLine1);  // display the line
      }
      br1.close();
    
    
    
        //Now read the second file or make for this separate method
          // read and display2
        String buffer2 = "";
    
        ArrayList<String> firstFile2 = new ArrayList<String>();
        while ((buffer2 = br2.readLine()) != null) {
        	firstFile2.add(buffer2);
          System.out.println(inLine2);  // display the line
      }
      br2.close();
    
        //Read all the lines in array or list
        //After that you can calculate them.
     }
    
    }
    But I got two errors below.
    Java Code:
    ReadTextFiles.java:33: variable inLine1 might not have been initialized
          System.out.println(inLine1);  // display the line
                             ^
    ReadTextFiles.java:46: variable inLine2 might not have been initialized
          System.out.println(inLine2);  // display the line
                             ^
    2 errors
    Regarding the while loop, do I need to create another while loop again when additional reading of textfile is implemented? Anyway to make the program shorter?
    Last edited by matt_well; 07-19-2008 at 01:48 AM.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    variable inLine1 might not have been initialized
    If you assign a value to the variable it will be initialized and make the compiler happier.
    do I need to create another while loop
    If there is another file to read, that would be a way to get all of its contents into your program. I thought that there were only 2 input files.???
    Assuming the two files had the same number of records in them, the next loop would be to go thru the two places you stored the input from the files, convert the Strings to numbers, add them and report the results.

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

    Default

    Quote Originally Posted by Norm View Post
    If you assign a value to the variable it will be initialized and make the compiler happier.

    If there is another file to read, that would be a way to get all of its contents into your program. I thought that there were only 2 input files.???
    Assuming the two files had the same number of records in them, the next loop would be to go thru the two places you stored the input from the files, convert the Strings to numbers, add them and report the results.
    By the way, inLine1 and inLine2 are String rite? Is it to set them to zero?
    Can I know how should I initialized inLine1 and inLine2 in this case?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    A couple of techniques to make the compiler happy:

    String inLine1 = null; // null value
    String inLine1 = ""; // empty string

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

    Default

    Quote Originally Posted by Norm View Post
    A couple of techniques to make the compiler happy:

    String inLine1 = null; // null value
    String inLine1 = ""; // empty string
    Original code,
    Java Code:
        String inLine1, inLine2;
    I tried to initialized both inLine1 and inLine2 which declared in the main part there to either null or "", then I get either all null or all blank.

    Modified code,
    Java Code:
    String inLine1=null; 
    String inLine2=null;
    or
    Java Code:
    String inLine1=""; 
    String inLine2="";
    How come it did not shows the results of values in both textfiles at the outputs there.

  20. #20
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  2. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  3. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:15 AM
  4. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 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
  •