Results 1 to 4 of 4
  1. #1
    movsesinator is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Sorting printed ArrayList of user inputted strings.

    My program asks for 3 parameters for a virtual portfolio; song title, date, and number of instrumental/miscellaneous parts.
    After these are inputted, the program prints them onto the screen and asks if you wish to continue "composing" ie; adding more songs. If not, the program should say how many songs are in the tracklist and print out the ArrayList of tracks in alphabetical order.
    I have every aspect completed in the program except for the alphabetical sorting. This is what i've tried.

    You can compile this in bluej or dr java or whatever you use if you want to see how it works.


    import java.util.*;

    public class Portfolio implements Comparable
    {
    private String title;
    private String date;
    private int parts;


    // -------------------------------------------------------
    // Creates a new portfolio with the given information.
    // -------------------------------------------------------
    public Portfolio (String songTitle, String songDate, int songParts)
    {
    title = songTitle;
    date = songDate;
    parts = songParts;
    }


    // -------------------------------------------------------
    // Returns a string with the information about the item
    // -------------------------------------------------------
    public String toString ()
    {


    return ("title: " + title + "\t\t" + "Date composed: " + (date) + "\t Instruments: " + parts + "\t");
    // + (date*quantity));
    }

    // -------------------------------------------------
    // Returns the date the song was created
    // -------------------------------------------------
    public String getDate()
    {
    return date;
    }

    // -------------------------------------------------
    // Returns the title of the composition
    // -------------------------------------------------
    public String getTitle()
    {
    return title;
    }

    // ----------------------------------------------------------------------------
    // Returns the number of instrumental/miscellaneous parts in the composition
    // ----------------------------------------------------------------------------
    public int getParts()
    {
    return parts;

    }
    // -------------------------------------------------
    // Sorts the compositions alphabetically by title
    // -------------------------------------------------
    public int compareTo(Object other)
    {
    int result;
    if (title.equals(((Portfolio)other).title))
    result = date.compareTo(((Portfolio)other).date);
    else
    result = title.compareTo(((Portfolio)other).title);
    return result;
    }
    }
    ____________________
    Now the main program:
    ____________________

    import java.util.Collections;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.ListIterator;

    public class Track
    {

    public static void main (String[] args)
    {
    Scanner scan = new Scanner (System.in);
    ArrayList<Portfolio> tracks = new ArrayList<Portfolio>();


    //Defines the elements in the array
    String songTitle;
    String songDate;
    int songParts;
    int total = 0;

    //Defines the user input necessary to run the program. Action is in the do-while loop.
    String keepWriting = "y";



    //Do while loop
    do
    {
    //Asks for the name of the composition and goes to next line.
    System.out.print ("Enter the name of the song or composition: ");
    songTitle = scan.nextLine();

    //Asks for the date at which the composition was created and goes to next line.
    System.out.print ("Enter the date: ");
    songDate = scan.nextLine();

    //Asks for the number of instrumentla or miscellaneous parts in the song.
    System.out.print ("Enter the number of parts in the song: ");
    songParts = scan.nextInt();
    scan.nextLine();

    // Creates a new song and adds it to the tracklist
    Portfolio portfolio = new Portfolio(songTitle, songDate, songParts);
    tracks.add (portfolio);


    // Prints the contents of the tracks object using println
    total += (portfolio.getParts());
    ListIterator iterator = tracks.listIterator();
    while (iterator.hasNext())

    System.out.println(iterator.next());




    //Asks if the user wishes to continue programming.
    System.out.print ("Continue composing (y/n)? ");
    keepWriting = scan.nextLine();
    }
    //End of the do while loop. Continues the programming if "y" was inputed. Otherwise, prints out
    //the current contents of the array.

    while (keepWriting.equals("y"));
    System.out.println("There are " + tracks.size() + " songs in your tracklist" + "\n" + tracks);

    System.out.println("Keep the tunes a'comin!");


    }

    }

    The compareTo method in the portfolio class does not work for me. I want to sort the ArrayList ONLY if the user no longer wants to add more songs to the tracklist.

    THANKS A MILLION to anyone who goes through my code and helps me out!! :p

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    I don't see your code sorting anything; after the while loop add a line:

    Java Code:
    Collections.sort(tracks);
    kind regards,

    Jos
    Last edited by JosAH; 04-03-2010 at 07:35 PM.

  3. #3
    movsesinator is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks a bunch i didnt realize the solution was so simple!
    praise to all the preexisting methods available in java :P

  4. #4
    movsesinator is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    another question and sorry for the double post.
    how could the program detect any other input when the program asks "Continue composing (y/n)?" besides "y" or "n"

    IE: how could it say something like "wrong choice" if the user types in "w" or something
    and ask again if the user wants to continue?

    Thanks again

Similar Threads

  1. Sorting an ArrayList
    By flesh-bound-book in forum New To Java
    Replies: 3
    Last Post: 02-13-2010, 12:20 PM
  2. While loop comparing strings from user
    By N3VRMND in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 08:18 AM
  3. getting strings from arraylist
    By Ms.Ranjan in forum New To Java
    Replies: 15
    Last Post: 05-06-2009, 10:40 PM
  4. Sorting strings in java?
    By alexander.s in forum New To Java
    Replies: 10
    Last Post: 09-01-2008, 04:14 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM

Tags for this Thread

Posting Permissions

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