Results 1 to 2 of 2
Thread: Help to sort alphabetically.
- 04-06-2012, 11:47 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
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(); } } }
- 04-07-2012, 02:48 AM #2
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
-
Sort a list alphabetically and split it into groups (From A to E, From F to J....)
By solariums in forum New To JavaReplies: 5Last Post: 12-21-2011, 03:39 AM -
Sort by Word frequency and alphabetically
By darpan12 in forum New To JavaReplies: 3Last Post: 01-06-2011, 06:26 PM -
Arraylist problem Sort Date , alphabetically
By ob3lix in forum New To JavaReplies: 1Last Post: 11-26-2010, 03:55 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Largest string value (alphabetically)
By mew in forum New To JavaReplies: 3Last Post: 12-14-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks