Results 1 to 12 of 12
Thread: Help with training program
- 12-24-2010, 01:02 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Help with training program
I am teaching myself Java right now and a friend of mine gave me what he calls a good "Training" program to develop. It is a console based app which needs to meet the following criteria:
1) Read in a text document called Records
a. The document will contain 10 lines of data organized like
1. First Name, Last Name, GPA
2) Display the contents of the file on the console with a column header for each category and each columns should be aligned.
3) Then just wait for user input
4) Then display the contents of the file alphabetized by last name while keeping alignment
5) Then just wait for user input
6) Then display the contents of the file arranged by GPA from highest to lowest while keeping alignment
7) Wait for user input before close
Below is what I have managed to come up with by looking online and adding what I already know. As you can see I am able to read in the file and display the contents, I am even able to alphabetize it (I don't know how to wait for user input before it alphabetizes). The problem is that I don't know how to make it alphabetize by last name or arrange by GPA, it just does it on the letters starting at the left side of the screen and working to the right (which essentially means it is alphabetizing by first name...and that isn't useful for me). I also don't know how to arrange the data into what I am going to call "left-aligned columns."
So what I need help with is:
1) How to alphabetize by last name and arrange me GPA.
2) How to display the contents of the file in "left-aligned columns."
3) How to wait for user input before rearranging/closing the console.
ps I am using Netbeans IDE for this and I appreciate any help that you can offer. :)
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Training; import java.io.*; class Main { public static void main(String args[]) { try{ // Open the file called Records FileInputStream fstream = new FileInputStream("Records.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String[] myarray; myarray = new String[10]; // Create a for loope that will read each line of the file // and place each line in an array for (int j = 0; j < myarray.length; j++){ myarray[j] = br.readLine(); // Display each line of the text document // This is only here right now to help me with troubleshooting System.out.println(myarray[j]); } //Close the input stream in.close(); // Sort the names alphabeticly and print out the sorted list. System.out.println("\n"); // I just wanted a space between this and the first print out java.util.Arrays.sort(myarray); for (int x = 0; x < myarray.length; x++){ System.out.println(myarray[x]); } }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } }
- 12-24-2010, 01:41 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Use and ArrayList and sort them.
With use of String.format() you can align them along with tabs. Have a look at on API
Use a conditional loop, a while loop, and depend on the user input control the rest. For an example you can use a while loop to continue or exit the program by the user.
- 12-24-2010, 01:56 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Thanks for your quick reply. :)
I know how to use .sort to alphabetize an array, the problem is that since each line of the file is organized by First Name, Last Name, GPA, the .sort only alphabetizes by First Name.
How do I get it to arrange each line of data based on the Last Name?
- 12-24-2010, 02:06 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
You mean that you want to sort them in Last Name, within that in First Name, and in GPA ? If not can you show it to me with an example, how your file looks like.
- 12-24-2010, 02:29 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
James, Carmichael, 2.1
George, Bush, 3.9
Brandon, Evanston, 4.0
Kevin, Newton, 1.8
Luke, Annihal, 2.3
Isaiah, Dunsfort, 2.5
Allen, Stance,3.5
Coke, Zero, 3.0
Pepsi, Cola, 3.3
Dew, Mountain, 2.0
That is how the file will look.
This is the code I use to place each line of the file into an array:
Java Code:for (int j = 0; j < myarray.length; j++){ myarray[j] = br.readLine();
Java Code:java.util.Arrays.sort(myarray); for (int x = 0; x < myarray.length; x++){ System.out.println(myarray[x]); }
I can see how it would be confusing without seeing an example list, I hope this is clear now.
Thank you very much for continuing to help me. :)
- 12-24-2010, 02:38 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Can you show me the expected file output from the above?
- 12-24-2010, 02:38 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Is that your myarray contain all the above lines?
- 12-24-2010, 02:51 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Yes myarray does contain each line of the file. (I suppose I should give it a better name...)
When alphabetized by last name the output (to console not to file) would be arranged like:
Luke, Annihal, 2.3
George, Bush, 3.9
James, Carmichael, 2.1
Pepsi, Cola, 3.3
Isaiah, Dunsfort, 2.5
Brandon, Evanston, 4.0
Dew, Mountain, 2.0
Kevin, Newton, 1.8
Allen, Stance, 3.5
Coke, Zero, 3.0
- 12-24-2010, 06:53 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Then either you've to bring the last name to the beginning of the text line, or handle them separately. But I think it's bit of work to do. So bring the last name to front and sort them. When you write to file reorder them, since you've a comma separated it's easy to do.
- 12-24-2010, 04:05 PM #10
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
I'm glad that it seems like it will be easy to do. :)
So how would I do it?
- 12-25-2010, 01:48 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
How did you say it's easy without knowing how to do it. ;)
- 12-25-2010, 01:50 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
As the first step, take the last name to front of the text line and order them alphabetically. See how it could done, show your effort here.
Similar Threads
-
Android Training in San Francisco Bay Area
By learncomputer in forum Reviews / AdvertisingReplies: 0Last Post: 02-26-2010, 07:52 AM -
Java Fundamentals training class in SF
By Rooz in forum Java SoftwareReplies: 0Last Post: 10-23-2009, 03:41 AM -
Hibernate training
By Human Resources Hermitage in forum Reviews / AdvertisingReplies: 0Last Post: 05-28-2008, 06:57 PM
Bookmarks