Results 1 to 7 of 7
  1. #1
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Question help please: print specific object in ArrayList

    Hi guys,

    I am having difficulties with creating a method to successfully print an object in an ArrayList based on a keyword. (method @ line 86:PlayList Class).

    the method is able to print the value of the object but in hex e.g. Track@567fg1

    I have two Classes layout below:

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.ListIterator;
    import java.util.Collections;
    import java.util.*;
    
    public class PlayList
    {
        private ArrayList<Track> tracks;
        private String refNumPrint;
        /**
         * Performs creating the play list (Constructs the play list to allow adding of tracks)
         * 
         */
        public void PlayList()
        {
            tracks = new ArrayList<Track>();
    
        }
    
        public void addTrack(Track newTrack)
        {   
    
            tracks.add(newTrack);
        }
    
        /**
         * Number of Tracks in play list.
         */
        public int numberOfTracks()
        {
            return tracks.size();
        }
    
        /**
         * Remove Track from the play list.
         */
        public void trackRemoval()
        {
            int trackNumber;
            Scanner input = new Scanner (System.in);
            System.out.print ("Enter track number to [Remove]: "); 
            trackNumber = input.nextInt();
            if (trackNumber < 0)
            {
                // This is not a valid track Number, so do nothing.
            }
            else if(trackNumber < numberOfTracks())
            // This is a valid track number
                tracks.remove(trackNumber);
        }
    
        /**
         * Print all Track information stored in array
         */
        public void printTracks()
        {
            System.out.println("List of Tracks currently in playlist");
            System.out.println();
            if (numberOfTracks() <= 0)
            {
                System.out.println("+--------------------------------------------+");
                System.out.println("There are currently no tracks in the play list");
                System.out.println("+--------------------------------------------+");
                System.out.println();
            }
            else
            {
                for(Track track : tracks)
                {
                    track.trackDetailsComplete();
                }
                System.out.println("Number of Tracks in Play List: " + numberOfTracks());
            }
        }
    
         /**
         * Print out track with specific data
         */
        public void printTracksByRefNum()
        {
            String refNum;
            int index;
            Scanner input = new Scanner (System.in);
            System.out.print ("Please enter Track Reference Number: ");
            refNum = input.nextLine();
            for (index = 0;index < numberOfTracks(); index++)
            {
                if(refNum.equals(tracks.get(index).referenceNumber()))
                    System.out.printf(" ",tracks.get(index).toString());
    
            }
        }
    
    }
    Java Code:
    public class Track
    {
        /** Instance variables
         */
        // Reference number
        private String referenceNumber;
        // Track play length
        private int playLength;
        // Track name
        private String trackName;
        // Track artist's name
        private String artistsName;
        // Track genre
        private String genre;
    
        /**
         * Creates a new track with details defined by user input
         */
        public Track(String newReferenceNumber, String newTrackName, String newArtistsName, String newGenre, int newPlayLength)
        {   
            referenceNumber = newReferenceNumber;
            trackName = newTrackName;
            artistsName = newArtistsName;
            genre = newGenre;
            playLength = newPlayLength;
        }
    
        /**
         * Displays the track play length and appends seconds
         */
        public void playLength(int playLength)
        {
            String playLengthStr = "";
            playLengthStr = Integer.toString(playLength);
            System.out.println(playLengthStr + " Seconds");
        }
    
        /**
         * Displays the track details defined by the variables inputted by user
         */
        public void trackDetailsComplete()
        {     
            // Prints line for each variable with a value inputed by user
            //System.out.println ("Reference Number:  " + referenceNumber + " " + "Track Name:  " +
            //trackName + " " + "Artist's Name:  " + artistsName + " " + "Genre:  " + genre
            //+ " " + "Play Length:  " + playLength);
            System.out.println ("~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println ("Track Reference Number: " + referenceNumber);
            System.out.println ("Track Name: " + " " + trackName);
            System.out.println ("Artist's Name: " + " " + artistsName);
            System.out.println ("Play Length: " + " " + playLength);
            System.out.println ("Track Genre: " + " "+ genre);
            System.out.println ("~~~~~~~~~~~~~~~~~~~~~~~~~");
        }
        
        /**
         * Returns the track reference number
         */
        public String referenceNumber()
        {
            return referenceNumber;
        }
    
        /**
         * Returns track name
         */
        public String trackName()
        {
            return trackName;
        }
    
        /**
         * Returns track artist's name
         */
        public String artistsName()
        {
            return artistsName;
        }
    
        /**
         * Returns track play length
         */
        public int playLength()
        {
            // put your code here
            return playLength;
        }
    
        /**
         * Returns track genre
         */
        public String genre()
        {
            return genre;
        }
    
        /**
         * Input of Track Reference Name
         */
        public void inputReferenceNumber(String newReferenceNumber)
        {
            referenceNumber = newReferenceNumber;
            int size = newReferenceNumber.length();        
            // Checks if reference name given to the track instance meets minimum requirements of three(3) charcters or more and if not, 
            //notifies user
            if (size <= 2)
            {
                System.out.println("Please re-input Track reference name");
                System.out.println("As it does not meet the minimum requirement of three(3) characters or more");
            }
        }
    
        /**
         * Creates a new instance with given values
         */
        public void newInstance(String newReferenceNumber, String newTrackName, String newArtistsName, String newGenre, int newPlayLength)
        {
            referenceNumber = newReferenceNumber;
            playLength = newPlayLength;
            trackName = newTrackName;
            artistsName = newArtistsName;
            genre = newGenre;
            int size = newReferenceNumber.length();
    
            // Checks if reference name given to the track instance meets minimum requirements of three(3) charcters or more and if not, 
            //notifies user
            if (size <= 2)
            {
                System.out.println("Please re-input Track reference name");
                System.out.println("As it does not meet the minimum requirement of three(3) characters or more");
            }
    
        }
    }
    Thanks in advance
    Last edited by nignogs; 04-08-2011 at 10:30 PM. Reason: mistyped code brackets

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    System.out.printf(" ",tracks.get(index).toString());
    shouldn't there be a %s in those quotation marks?

    and tracks.get(index) returns an object, toString() would print a weird code for the object. you'd need to get some object attribute which contains a string or number and print that i.e.

    Integer.toString(tracks.get(index).getTrackID())

  3. #3
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Morning Ozzyman,

    I am not to sure what you mean, I am still a beginner. maybe there is a simpler way?

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    The output you're seeing is the result of Track's default toString() method. If you want to print something meaningful, you need to override that method with something like the following:

    Java Code:
    public class Track {
        ...
        public String toString() {
            return artistName + " " + trackName;
        }
    }

  5. #5
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Hi Iron Lion,

    Thanks for that I can see clearly now were i went wrong;

    As you can see I have another print method printTracks() which calls trackDetailsComplete() .

    thanks I am learning everyday :)

  6. #6
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Hi again,

    I modified the the method even further: saves having to create the toString method as the method trackDetailsComplete works fine for returning nicely formatted output.

    Java Code:
         /**
         * Print out track with specific data
         */
        public void printTracksByRefNum()
        {
            String refNum;
            int index;
            Scanner input = new Scanner (System.in);
            System.out.print ("Please enter Track Reference Number: ");
            refNum = input.nextLine();
            for (index = 0;index < numberOfTracks(); index++)
            {
                if(refNum.equals(tracks.get(index).referenceNumber()))
                    //System.out.printf("%s",tracks.get(index).toString());
                    tracks.get(index).trackDetailsComplete();
    
            }
        }

    Thanks again though Iron Lion, it open my eyes that little more :)

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    when using the print formatter (printf), you write a complete string using % codes to tell the program where to print values in a string. for example, say we have these details to print:

    String aName = "John";
    int aNumber = 10;
    double aDouble = 7.50;

    To print the above details in one sentence, we could write something like this:
    System.out.printf("%s is %d years old and has $%.2f pocket money.",aName,aNumber,aDouble);

    The output for this sentence would be:

    Java Code:
    John is 10 years old and has $7.50 pocket money.

    Its much better than printing a series of print/println's to get your desired result.

Similar Threads

  1. get specific arrayList from collection
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 03-23-2011, 04:11 AM
  2. Multiplying specific data within a arraylist
    By fiefa in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 08:54 AM
  3. Retrieving specific objects from ArrayList
    By soketti in forum New To Java
    Replies: 49
    Last Post: 10-29-2009, 07:10 AM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  5. Replies: 2
    Last Post: 11-08-2008, 10:40 AM

Posting Permissions

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