Results 1 to 2 of 2
  1. #1
    bry129 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Help to sort alphabetically.

    This code is suppose to ask the user for two input txt files and an output txt file.
    Merge the two input txt files into one.
    Sort alphabetically.
    Format:
    Last, First
    ID
    Last, First
    ID
    ...ect
    Its suppost to print that into an output text file that will have all the names and id's in alphabetical order.

    Atm. It is only printing out the First correct name and its id and not doing the rest. Help please thanks!

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.io.*;
    
    public class Student_Records
    {
    		public static void main (String [] args)throws IOException
    		{
    				String inputString;
    				String fileOne, fileTwo, output;
    				
    				fileOne = JOptionPane.showInputDialog("Enter the name of the first input file including \".txt\" at the end: ");
    				fileTwo = JOptionPane.showInputDialog("Enter the name of the second input file including \".txt\" at the end: ");
    				output = JOptionPane.showInputDialog("Enter the name for the output file including \".txt\" at the end: ");
    								 
    				combiner(fileOne, fileTwo);
    				sorter(fileTwo, output);			 
    							 
    				System.exit(0);
    		}
    		
    		// combines the two input files together
    		
    		public static void combiner(String one, String two)throws IOException
    		{
    				// Open the file for reading
    				File file = new File(one);
    				Scanner inputFile = new Scanner(file);
    				
    				while (inputFile.hasNext())
    				{
    						String info = inputFile.nextLine();
    				
    						FileWriter fwriter = new FileWriter(two,true);
    						PrintWriter outputFile = new PrintWriter(fwriter);
    						outputFile.println(info);
    						outputFile.close();
    				}
    				
    		}
    		
    		//Sorts the file by name and prints to the file
    		
    		public static void sorter(String two, String list)throws IOException
    		{
    				// Opens hte file for reading
    				File file = new File(two);
    				Scanner inputFile = new Scanner(file);
    				
    				while (inputFile.hasNext())
    				{
    						String name = " ", id = " ";
    						String nameOne = inputFile.nextLine();
    						String nameOne_id  = inputFile.nextLine();
    						String nameTwo = inputFile.nextLine();
    						String nameTwo_id  = inputFile.nextLine();
    						
    						while (inputFile.hasNext())
    						{
    								if (nameOne.compareTo(nameTwo) < 0)
    								{
    										name = nameOne;
    										id = nameOne_id;
    										nameTwo = inputFile.nextLine();
    										nameTwo_id  = inputFile.nextLine();
    										
    								}
    								else if (nameOne.compareTo(nameTwo) > 0)
    								{
    										name = nameTwo;
    										id = nameTwo_id;
    										nameOne = inputFile.nextLine();
    										nameOne_id  = inputFile.nextLine();
    								}
    						}
    						 
    						System.out.println(name);
    						System.out.println(id);
    						FileWriter fwriter = new FileWriter(list,true);
    						PrintWriter outputFile = new PrintWriter(fwriter);
    						outputFile.println(name);
    						outputFile.println(id);
    						outputFile.close();
    				}
    		}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: Help to sort alphabetically.

    Can you post the current output and add comments explaining what is wrong with the output and also show what the output should look like.

    Can you explain the logic of your code. It does not make sense to me. Make a list in pseudo code of what you want the statements in the code to do.

    Your parameter names to the methods are poor. They should describe what the variables contain like inFileName and outFileName. Then when the code is read you know what the variable contains and what it should be used for.
    When I see the name: one it has no meaning.
    Last edited by Norm; 04-07-2012 at 02:55 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 03:39 AM
  2. Sort by Word frequency and alphabetically
    By darpan12 in forum New To Java
    Replies: 3
    Last Post: 01-06-2011, 06:26 PM
  3. Arraylist problem Sort Date , alphabetically
    By ob3lix in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 03:55 PM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. Largest string value (alphabetically)
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •