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