#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Write a program that merges two files containing alphabetized lists of student record


    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.

    Code:
    public static void main(String[] args) throws IOException  
        {    
            // Asks the user to input a file in a dialog box
            
            String fileName1 = JOptionPane.showInputDialog("Enter filename: " ); 
            String fileName2 = JOptionPane.showInputDialog("Enter filename: " );
            String studentName1 = "";
            String studentName2 = "";
            
            PrintWriter outputFileWriter = new PrintWriter("Students.txt");
            
            File file1 = new File(fileName1);  
            Scanner inputFile1 = new Scanner(file1); 
            File file2 = new File(fileName2);  
            Scanner inputFile2 = new Scanner(file2);
            
            // while loop reading each line of inputFile1
            
            while(inputFile1.hasNext())     
            {      
                studentName1 = inputFile1.nextLine();      
                studentName2 = null;      
            
                while (inputFile2.hasNext())     
                {      
                    studentName2 = inputFile2.nextLine();      
                        
                    if (studentName1.compareTo(studentName2) < studentName2.compareTo(studentName1))     
                    {      
                        outputFileWriter.println(studentName2);    
                    }      
                }      
                    
                if (studentName2.compareTo(studentName1) > studentName1.compareTo(studentName2))     
                {      
                    outputFileWriter.println(studentName1);    
                }      
            }        
              
              
            // closes print writer
            outputFileWriter.close(); 
        }
    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at assignment.pkg4.Assignment4.main(Assignment4.java:55)
    Java Result: 1
    What do I need to do for it to work properly?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Exception in thread "main" java.lang.NullPointerException
    at assignment.pkg4.Assignment4.main(Assignment4.java:55)
    Look at line 55 and see what variable has a null value. Then backtrack in the code to find out why that variable does not have a valid value.
    What do I need to do for it to work properly?
    Give the variable with the null value a valid value.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Look at line 55 and see what variable has a null value. Then backtrack in the code to find out why that variable does not have a valid value.

    Give the variable with the null value a valid value.
    Ok, can I do this?

    Code:
    studentName2 = "";
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Did you try it? What happened?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    Yeah, it runs and compiles. The only thing wrong is that it is not putting the right contents in "students.txt".

    I have two files "inTest1.txt" and "inTest2.txt" that are already alphabatized. I just can't seem to get them combined and in an alphabatized order.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    it is not putting the right contents
    Can you show the input to the program and its output that shows the problem?
    Add some comments to show what is wrong.

    To make the testing of your merge easier I suggest you rewrite the code for testing the merge code:
    Code:
            Scanner inputFile1 = new Scanner("A\nC\nG\n");
            Scanner inputFile2 = new Scanner("B\nF\nH\n");
            
            // while loop reading each line of inputFile1
            
            while(inputFile1.hasNext())        {      
                String studentName1 = inputFile1.nextLine();      
                String studentName2 = "";      
            
                while (inputFile2.hasNext())             {      
                    studentName2 = inputFile2.nextLine();      
                        
                    if (studentName1.compareTo(studentName2) < studentName2.compareTo(studentName1))     {      
                        System.out.println(studentName2);    
                    }      
                }      
                    
                if (studentName2.compareTo(studentName1) > studentName1.compareTo(studentName2))  {      
                    System.out.println(studentName1);    
                }      
            }
    Last edited by NormR; April 6th, 2012 at 05:11 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Can you show the input to the program and its output that shows the problem?
    Add some comments to show what is wrong.
    Sure, this post may get lengthy though.

    Code:
    String fileName1 = JOptionPane.showInputDialog("Enter filename: " ); 
    String fileName2 = JOptionPane.showInputDialog("Enter filename: " );
    
    User enters fileName1 = inTest1.txt
    User enter fileName2 = inTest2.txt
    OutputFile students.txt

    Code:
    Ones in RED are from inTest2.txt and Anderson, James is the only one from 
    inTest1.txt to be printed to the file. They should be in alphabetize order but 
    Anderson, James clearly isn't.
    
    Ables, Kristi
    Anderson, Brandon
    Andrews, Tisha
    Boyd, Billy Bob
    Brown, Jessica
    Galleger, Mary Catherine
    Longoria, Maria
    Smith, Andrew
    Smith, Jake
    Tanner, Thomas
    Thomas, Shannon
    Thompson, Taylor 
    Tran, Jenny
    Anderson, James
    I changed a few things from the first post code.

    Code:
    public static void main(String[] args) throws IOException  
        {    
            // Asks the user to input a file in a dialog box
            
            String fileName1 = JOptionPane.showInputDialog("Enter filename: " ); 
            String fileName2 = JOptionPane.showInputDialog("Enter filename: " );
            String studentName1 = "";
            String studentName2 = "";
            
            PrintWriter outputFileWriter = new PrintWriter("Students.txt");
            
            File file1 = new File(fileName1);  
            Scanner inputFile1 = new Scanner(file1); 
            File file2 = new File(fileName2);  
            Scanner inputFile2 = new Scanner(file2);
            
            // while loop reading each line of inputFile1
            
            while(inputFile1.hasNext())     
            {      
                studentName1 = inputFile1.nextLine();      
                studentName2 = "";      
            
                while (inputFile2.hasNext())     
                {      
                    studentName2 = inputFile2.nextLine();      
                        
                    if (studentName1.compareTo(studentName2) < 0)     
                    {      
                        outputFileWriter.println(studentName2);    
                    }
                    else
                    {
                        outputFileWriter.println(studentName2);
                    }
                }      
                    
                if (studentName2.compareTo(studentName1) > 0)     
               {      
                    outputFileWriter.println(studentName1);    
                }      
            }        
              
              
            // closes print writer
            outputFileWriter.close(); 
        }
    I don't understand what you mean by rewriting it. I know something is wrong but not sure what it is.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Before you spend any more time with the code you have written, you need to stop and do some design work to get better logic. Write the logic for how the program should work in pseudo code before doing any more coding. You need to have a design before writing any more code.
    Post the pseudo code to solve the problem so we can look at it and see if it will do the job.


    When you are ready to write the new code,
    Use my test program for testing the merge logic. It makes testing much simpler.

IMN logo majestic logo threadwatch logo seochat tools logo