Results 1 to 12 of 12
  1. #1
    hittman32 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Question 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());
    		}
    
    	}
    
    
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by hittman32 View Post
    1) How to alphabetize by last name and arrange me GPA.
    Use and ArrayList and sort them.

    Quote Originally Posted by hittman32 View Post
    2) How to display the contents of the file in "left-aligned columns."
    With use of String.format() you can align them along with tabs. Have a look at on API

    Quote Originally Posted by hittman32 View Post
    3) How to wait for user input before rearranging/closing the console.
    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.

  3. #3
    hittman32 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    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?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  5. #5
    hittman32 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    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();
    This is the code I use to sort the file alphabetically and then display it to the console:
    Java Code:
                    java.util.Arrays.sort(myarray);
                    for (int x = 0; x < myarray.length; x++){
                    System.out.println(myarray[x]);
                    }
    So when I use sort it only uses the first name and I need to sort each line by last name and then by GPA.
    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. :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    hittman32 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  10. #10
    hittman32 is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    I'm glad that it seems like it will be easy to do. :)

    So how would I do it?

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Android Training in San Francisco Bay Area
    By learncomputer in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-26-2010, 07:52 AM
  2. Java Fundamentals training class in SF
    By Rooz in forum Java Software
    Replies: 0
    Last Post: 10-23-2009, 03:41 AM
  3. Hibernate training
    By Human Resources Hermitage in forum Reviews / Advertising
    Replies: 0
    Last Post: 05-28-2008, 06:57 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
  •