Reputation: 161
I have to write a program that reads from two files that each have a list of names and IDs that are both already in alphabetical order based on last name. The program is then suppose to create a third file that the user names that contains a complete list of all the names and IDs from the two programs and puts them in alphabetical order. The format of the files are as such--
Last name,First name
ID(ex.rbb091020)
Last name, First name
ID
My program reads the two files, and creates a third, but for some reason, it doesn't write anything to the third file, and the file is left blank. What can I do to fix this?
Here is my code...
import java.io.*;
import java.util.Scanner;
public class Combine_Files
{
public static void main(String[]args) throws IOException
{
String filename1 = "";
String filename2 = "";
String combinedFileName = "";
String response = "";
String nextName1 = "";
String nextName2 = "";
String nextIDNumber1 = "";
String nextIDNumber2 = "";
boolean okToOverwrite = false;
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the first file?");
filename1 = keyboard.nextLine();
File file1 = new File(filename1);
if (!file1.exists())
{
System.out.println(file1 + " does not exist.\nTerminating Program.");
System.exit(0);
}
System.out.println("What is the name of the second file?");
filename2 = keyboard.nextLine();
File file2 = new File (filename2);
if (!file2.exists())
{
System.out.println(file2 + " does not exist.\nTerminating Program.");
System.exit(0);
}
System.out.println("What is the name of the file that you wish to create?");
combinedFileName = keyboard.nextLine();
File combinedFile = new File (combinedFileName);
while (combinedFile.exists() && !okToOverwrite)
{
System.out.println("\nError, a file named " +
combinedFileName + " already exists." +
"\nWould you like to overwrite this file?" +
"\nEnter Y for Yes or N for No.");
response = keyboard.nextLine();
// Add input validation on response
if (response.equalsIgnoreCase("Y"))
{
okToOverwrite = true;
}
else
{
System.out.println("Enter a new filename.");
combinedFileName = keyboard.nextLine();
combinedFile = new File(combinedFileName);
}
}
if (file1.exists() && file2.exists())
{
Scanner list1 = new Scanner(file1);
Scanner list2 = new Scanner(file2);
PrintWriter outputFile = new PrintWriter(combinedFile);
while (list1.hasNext() && list2.hasNext())
{
nextName1 = list1.nextLine();
nextName2 = list2.nextLine();
if(nextName1.compareToIgnoreCase(nextName2) <0)
{
outputFile.print(nextName1);
nextName1 = list1.nextLine();
outputFile.print(nextName1);
}
else if(nextName1.compareToIgnoreCase(nextName2) >0)
{
outputFile.println(nextName2);
nextName2 = list2.nextLine();
outputFile.println(nextName2);
}
else
{
outputFile.println(nextName1);
nextName1 = list1.nextLine();
outputFile.println(nextName1);
outputFile.println(nextName2);
nextName2 = list2.nextLine();
outputFile.println(nextName2);
}
}
while (list1.hasNext() && !list2.hasNext())
{
outputFile.println(nextName1);
}
while (list2.hasNext() && !list1.hasNext())
{
outputFile.println(nextName2);
}
outputFile.close();
}
}
}
Upvotes: 0
Views: 4073
Reputation: 13153
I found two errors: the main one is that the loops at the bottom which are to output lines in one input file after the other input file is exhausted do not advance the files they are reading. So those loop without end if they execute at all.
The second error is the use of "print()" instead of "println()" after the first name comparison; this outputs the line without an end-of-line, and is probably not what you want.
After that I got output, though I did not examine it for other bugs.
If I may suggest: You could make your program simpler if you used a few more methods that performed common functions. For instance: one method could take the input already read, one input scanner and the output file as arguments and write out the current name. Something like
private void writeName(String currentLine, Scanner inFile, File outFile)
{
outFile.println(currentLine);
String nextLine = inFile.nextLine();
outFile.println(nextLine);
}
The way to spot these is to be aware of when you seem to be doing the same coding steps more than once. In this case, you are doing the above steps separately for the two input files, so you can eliminate the "copy and paste" nature of the code with this method (and others like it.
Upvotes: 1