Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Write a program that merges two files containing alphabetized lists of student records into a single Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Write a program that merges two files containing alphabetized lists of student records into a single" Watch "Write a program that merges two files containing alphabetized lists of student records into a single" New topic
Author

Write a program that merges two files containing alphabetized lists of student records into a single

brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
My program doesn't ask for the second file. This is just the start and I note that I am not a great programmer so bare with me. I cannot use arrays because we haven't covered them yet.

Problem I am trying to solve:

Write a program that merges two files containing alphabetized lists of student records into a
single file containing a list of alphabetized student records.

Your program must ask the user to enter the names of two input files and a single output file
which will contain the merged contents of the two input files. Use dialog boxes to get the
filenames from the user. If your program does not allow us to enter filenames so that we can run
your program on various input files specified at runtime, you will lose a large percentage of the
points for the execution of your program. Include code to avoid the exception that occurs when
an attempt is made to open an input/source file that does not exist. Include code so that you do
not overwrite an existing output file without first getting the user’s permission.

Your program must work for any input files in the format specified. Do not make any
assumptions about the number of student records in the files. An input file can consist of any
number of student records zero or more. The records in the files are in ascending alphabetical
order by last name and then first name.

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Not sure if that's the problem, but lines 21 and 22 refer to the first file, not the second one:
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
I fixed that but it still doesn't ask me to enter file 2.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

Everyone here wants to help you. But when you post a question here, you need to make it as easy as possible for us to help you. That means

1) post code that actually compiles. Yours doesn't. It needs several import statements added.
2) Don't just say "It doesn't work". Tell us what it DOES. Tell us what you EXPECT. be specific. I would expect you to say something like

"After I input the first file name, the program seems to hang. It doesn't ask for anything else. Further, if I type anything on the command line where I started it, I get an exception:
Exception in thread "main" java.io.FileNotFoundException: a (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Assignment4.main(Assignment4.java:17)
"

as to fixing your problem...your best friend is "System.out.println()". if your program hangs, put a ton of them in all over the place to see what your code is doing, and exactly WHERE it is hanging. Something like this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Try this instead:
You don't need the keyboard1 and keyboard2 Scanner objects since you already get the filename returned from the input dialog.
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Something like this:



After doing what you mentioned my program only got to "got here 1". My IDE is still running.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Your program must ask the user to enter the names of two input files and a single output file
which will contain the merged contents of the two input files. Use dialog boxes to get the
filenames from the user.


Why are you iterating through file1 before asking the user for file2?

The assignment verbosely explains the deliverables (I wish my PMs would do the same)

Why not p-code (psuedo code) how you should approach it and maybe we can help you with the Java aspects.

WP
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Get rid of your keyboard1 and keyboard2 Scanners like I mentioned before. Your code first get input from the dialog, then it waits on the console for input again.
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Koen Aerts wrote:Get rid of your keyboard1 and keyboard2 Scanners like I mentioned before. Your code first get input from the dialog, then it waits on the console for input again.


Ok, I did that but I have errors on this line:

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

brent bynum wrote:
Koen Aerts wrote:Get rid of your keyboard1 and keyboard2 Scanners like I mentioned before. Your code first get input from the dialog, then it waits on the console for input again.


Ok, I did that but I have errors on this line:


Come on, I practically gave the solution already. Change this:
to this:
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Koen Aerts wrote:
brent bynum wrote:
Koen Aerts wrote:Get rid of your keyboard1 and keyboard2 Scanners like I mentioned before. Your code first get input from the dialog, then it waits on the console for input again.


Ok, I did that but I have errors on this line:


Come on, I practically gave the solution already. Change this:
to this:


I'm sorry I did not understand.


Let me work on the rest of the program. I post back with my progress.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

What Koen is saying is that basically, your code is trying to get the name of file A from the user TWICE.

You pop up a dialog box asking for the file name. The user enters "filename1.txt". When they close that dialog box, it prints "got here 1". You then have a Scanner object that is waiting for input from the keyboard.

I don't use an IDE (at least, not for this). I ran the code from the command line. so, in my cmd.exe window, I typed "filename1.txt" again, and the program proceeded.

It's like you asking someone to write the name of a file on a slip of paper, and then once they do, you are waiting for them to tell you verbally. You don't need to do both.
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
I have it asking for the two files and creating students.txt to put the contents in it. I am not sure how to alphabatize them and put them in students.txt.

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Your assignment mentions the names in the 2 input files are already sorted. So now you have to iterate through the lines in the 2 input files; for each line you read from the files, you compare them with each other, then write the "smaller" one to the output file. To achieve this you could try a loop like this:
To compare the 2 strings you could use the compareTo() String method. Each time studentName1 is less than studentName2, you write studentName1 to the output file and break out of the while (inputFile2) loop. Otherwise write out studentName2 to the output file and don't break out of the while (inputFile2) loop.
brent bynum
Greenhorn

Joined: Apr 04, 2012
Posts: 24
Koen Aerts wrote:Your assignment mentions the names in the 2 input files are already sorted. So now you have to iterate through the lines in the 2 input files; for each line you read from the files, you compare them with each other, then write the "smaller" one to the output file.

To compare the 2 strings you could use the compareTo() String method. Each time studentName1 is less than studentName2, you write studentName1 to the output file and break out of the while (inputFile2) loop. Otherwise write out studentName2 to the output file and don't break out of the while (inputFile2) loop.




My compiler is saying:


Am I on the right track though?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write a program that merges two files containing alphabetized lists of student records into a single
 
Similar Threads
Help with number guessing game
java redirection
getting java.lang.NoSuchMethodError: main error! thanks =)
Inputting both fixed-length strings and 32-bit integers from the same file
can some one help me im just new in java.