Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I'm trying to combine 2 files into 1. Please help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I'm trying to combine 2 files into 1. Please help

    I need to take 2 different files and combine them into 1 file. This is what I have so far, but I'm doing something wrong. The error I get is java.util.NoSuchElementException: No line found (in java.util.Scanner) on this line 'String line2 = file2.nextLine();' Also I have stuff in the file but everytime this runs my file empties. Please help, I'm stuck! Here is my code.

    public static void combineAandQ(File file) throws java.io.IOException {
                   Scanner file1 = new Scanner(new File("C:\\Users\\me\\questions.txt"));
                   Scanner file2 = new Scanner(new File("C:\\Users\\me\\answers.txt"));
                     FileWriter fw = new FileWriter("C:\\Users\\me\\combo.txt");
                     BufferedWriter bw = new BufferedWriter(fw);
                     PrintWriter pw = new PrintWriter(bw);
                     while (file1.hasNext() ){
                 String line1 = file1.nextLine();
                 String line2 = file2.nextLine();
                 System.out.println(line1); 
                 System.out.println(line2);        
                 String line3 = line1.concat(" " + line2);
                 pw.println(line3);
                    pw.close();  
            }
                }//end of combineAandQ


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,107
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: I'm trying to combine 2 files into 1. Please help

    Your code needs to test that both Scanner objects has a next line. You are only testing one.
    You are assuming that the first file does not have more lines than the second file.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm trying to combine 2 files into 1. Please help

    Ok, I added another "while" for the 2nd file. I found in my main a piece of code that should not have been there that was erasing my answers file. Otherwise, there would've been the same number of lines in both. Because my 2nd file was being erased, that is why this code wasn't working. Thanks for your advice on testing the second file.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: I'm trying to combine 2 files into 1. Please help

    I'm trying to combine 2 files into 1. Please help

    when using file writer there is a way to tell it to append the file. (aka, add to the end of what is already there instead of overwriting it.)

    look up append to a file, if you don't need to evaluate or combine the data in the files.

Similar Threads

  1. [SOLVED] files size for files inside the JAR file
    By Natherul in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 02:17 PM
  2. How do I combine two strings in an array?
    By SkyAphid in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2011, 06:20 PM
  3. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  4. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  5. How to combine these 3 void methods?..
    By TimoElPrimo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 8th, 2011, 01:25 PM