• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if that's the problem, but lines 21 and 22 refer to the first file, not the second one:
 
brent bynum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed that but it still doesn't ask me to enter file 2.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Koen Aerts
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this:



After doing what you mentioned my program only got to "got here 1". My IDE is still running.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 was her plaything! And so was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic