Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Talking Reaaaallly Need Help With My Array Code! Any Takers? ;D

    This probably looks terrible, but it's my first experience with Java this year ;P. Everything works fine now except for the fact that when I run the program the values for Song#, songRating, and numberOfSongs all come out as 0. It's aggrevating. I know, I suck at this. I will gladly return a favor to anyone who can either give me some pointers or help me fix this before I cry lol. Thanks :)



    import java.util.Scanner;

    public class PlayListBuilderArray
    {
    public static void main(String[] args)
    {

    Scanner in = new Scanner(System.in);

    String inputText;

    int numberOfSongs = 0;



    do {
    System.out.print("How many songs would you like to add to the playlist? (Can only add up to 15) : ");
    inputText = in.nextLine();
    numberOfSongs = Integer.parseInt(inputText);

    if (numberOfSongs < 1)
    {
    System.out.println("ERROR - You can't enter "+numberOfSongs+" songs.");
    }
    else if (numberOfSongs > 15)
    {
    System.out.println("ERROR - Please enter 15 songs or less.");
    numberOfSongs = 0;
    }
    } while (numberOfSongs < 1);


    PlayList[] songList = new PlayList[numberOfSongs];

    for (int count = 0; count < songList.length; count++)
    {
    songList[count] = new PlayList();

    System.out.print("What is song #"+(count+1)+"\'s title? ");
    inputText = in.nextLine();
    songList[count].setSongTitle(inputText);

    System.out.print("Who is song #"+(count+1)+" written by? ");
    inputText = in.nextLine();
    songList[count].setArtistName(inputText);

    System.out.print("What is song #"+(count+1)+"\'s album titled? ");
    inputText = in.nextLine();
    songList[count].setAlbumTitle(inputText);

    System.out.print("What is song #"+(count+1)+"\'s rating? ");
    inputText = in.nextLine();


    }



    ArrayObjectTools.printArray(songList);

    for (int count = 0; count < songList.length; count++)
    {
    System.out.println("Song #"+(count)+":");
    System.out.println(songList[count]);
    System.out.println("-----------------------------------------");
    }

    }

    }


    Result of running the program if I chose only 1 song:
    -----------------------------------------
    Song #0:
    Artist Name: wanda
    Song Title: jade
    Album Title: portia
    Song Rating(stars): 0
    Number of Songs in Playlist: 0
    -----------------------------------------

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags.
    [code]
    YOUR CODE HERE
    [/code]

  3. #3
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    All done :) Also I figured almost all of them out now. I am only having problems with running it. For example if I enter that I am putting in 3 songs to the playlist, when I run the program, it still shows up as 0. I have corrected the Song # and Song Rating issue.


    Java Code:
    import java.util.Scanner;
    
    public class PlayListBuilderArray
    {
       public static void main(String[] args)
    	{
    	
    	   Scanner in = new Scanner(System.in);
    		
    		String inputText;
    		
    		int numberOfSongs = 0;
    		
    		
    		
    		do {
    		   System.out.print("How many songs would you like to add to the playlist?  (Can only add up to 15) : ");
    			inputText = in.nextLine();
    			numberOfSongs = Integer.parseInt(inputText);
    			
    			if (numberOfSongs < 1)
    			{
    			   System.out.println("ERROR - You can't enter "+numberOfSongs+" songs.");
    			}   
    			else if (numberOfSongs > 15)
    			{
    			   System.out.println("ERROR - Please enter 15 songs or less.");
    				numberOfSongs = 0;
    			}
    		} while (numberOfSongs < 1);
    
    
          PlayList[] songList = new PlayList[numberOfSongs];
    		
    		for (int count = 0; count < songList.length; count++)
    		{
    		   songList[count]  = new PlayList();
    			
    			System.out.print("What is song #"+(count+1)+"\'s title? ");
    		   inputText = in.nextLine();
    		   songList[count].setSongTitle(inputText);
    		
    			System.out.print("Who is song #"+(count+1)+" written by? ");
    		   inputText = in.nextLine();
    		   songList[count].setArtistName(inputText);
    			
    			System.out.print("What is song #"+(count+1)+"\'s album titled? ");
    		   inputText = in.nextLine();
    		   songList[count].setAlbumTitle(inputText);
    
             System.out.print("What is song #"+(count+1)+"\'s rating? ");
    		   inputText = in.nextLine();
    
    		
    		}
    
    	  
    	  
    	  ArrayObjectTools.printArray(songList);
    	  
    	  for (int count = 0; count < songList.length; count++)
    	  {
    		 System.out.println("Song #"+(count)+":");
    		      System.out.println(songList[count]);
    				System.out.println("-----------------------------------------");
    		}
    		
    	}
    	
    }

  4. #4
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Playlist Builder. Contains Constructor and such.

    Java Code:
    import java.util.Scanner;
    
    public class PlayListBuilder
    {
       public static void main(String[] args)
    	{
    	   Scanner in = new Scanner(System.in);
    		
    		String inputText;
    		
    		int numberOfSongs = 0;
    		
    		double sum = 0.0;
    		
    		do
    		{
    		   System.out.print("How many songs would you like to add to the playlist?  (Can only add up to 15) : ");
    			inputText = in.nextLine();
    			numberOfSongs = Integer.parseInt(inputText);
    			
    			if (numberOfSongs < 1)
    			{
    			   System.out.println("ERROR - You can't enter "+numberOfSongs+" songs.");
    			}
    			else if (numberOfSongs > 15)
    			     {
    			        System.out.println("ERROR - Please enter 15 songs or less.");
    					  numberOfSongs = 0;
    				  }				
    		} while (numberOfSongs < 1);
    		
    		PlayList[] songList = new PlayList[numberOfSongs];
    		
          for (int count = 0; count < numberOfSongs; count++)
    		{
    		   songList[count] = new PlayList();
    			
    			
    			System.out.print("Who is song #"+(count+1)+" written by? ");
    		   inputText = in.nextLine();
    		   songList[count].setArtistName(inputText);
    			
    			System.out.print("What is song #"+(count+1)+"\'s title? ");
    		   inputText = in.nextLine();
    		   songList[count].setAlbumTitle(inputText);
    		
    		   System.out.print("What is song #"+(count+1)+"\'s album titled? ");
    		   inputText = in.nextLine();
    		   songList[count].setSongTitle(inputText);
    
    		   System.out.print("What is song #"+(count+1)+"\'s rating? (From 1 - 5. With 1 being the lowest.) ");
    		   inputText = in.nextLine();
    		   songList[count].setSongRating(Integer.parseInt(inputText));
    			
    			  }
    		
          for (int count = 0; count < numberOfSongs; count++)
    		{
    		   System.out.println("Song #"+(count+1)+":");
    			System.out.println(songList[count]);
    			System.out.println("--------------------------------------");
    			
    		}
    		
    		System.out.println("The number of songs in this playlist are:  "+(numberOfSongs)+".");
    	}
    }

  5. #5
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    anyone? :(

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If no one has answered by then I will be able to give you advice in about 2 hours.

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I will give you an answer in about 45 minutes.

  8. #8
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Okay. Thank you so much. I really appreciate this!

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Where is the playlist class?

  10. #10
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default Begging of Code Source. With gets and sets.

    Ignore the commenting on this. I was using my last one to save a little time and will edit it later.

    Java Code:
    import java.util.Scanner; 
    
    public class PlayList 
    {
    		private String artistName; // This is the ipod model attribute of a Ipod object
    		private String songTitle; // This is the ipod color attribute of a Ipod object
    		private String albumTitle; // This is the title of the song's album
    		private int songRating; // This is the year of purchase attribute of a Ipod object
    		private int numberOfSongs; // This is the data storage size attribute of a Ipod object
    	
    				public PlayList() // Constructor. Any class can use this.
    		{
    			this("","","", 0,0); // Attribute variables.
    		}
    	 
    		public PlayList(String artistName, String songTitle, String albumTitle) // Strings ipodModel and ipodColor
    		{
    		this(artistName, songTitle, albumTitle, 0, 0); // Sets variables and original values.
    		}
    		
    		public PlayList(String artistName, String songTitle, String albumTitle, int songRating, int numberOfSongs) // Strings attributes to object.
    		{
    		setArtistName(artistName); // Defines method setIpodModel.
    			setSongTitle(songTitle); // Defines method setIpodColor.
    			setAlbumTitle(albumTitle); // Defines method setYearPurchased.
    			setSongRating(songRating); // Defines method setDataStorage.
    			setNumberOfSongs(numberOfSongs); // Defines method setNumberOfSongs
    		}
          
    	public void setArtistName(String artistName) // Method to set ipod model.
    {
       this.artistName = artistName; // Links ipodModel as an equal to ipodModel.
    }
    public void setSongTitle(String songTitle) // Method to set ipod color
    {
       this.songTitle = songTitle; // Links ipodColor as an equal to ipodColor.
    }
    public void setAlbumTitle(String albumTitle) // Method to set ipod model.
    {
       this.albumTitle = albumTitle; // Links ipodModel as an equal to ipodModel.
    }
    
    public void setSongRating(int ratingInStars) // Method to set year purchased.
    {
       songRating = ratingInStars;// Links yearPurchased as an equal to ageInYears.
    }
    public void setNumberOfSongs(int totalSongs) // Method to set data storage.
    {
    	numberOfSongs = totalSongs; // Links dataStorage as an equal to gigaBytes.
    }
    public String getArtistName() // Method that returns the ipodModel.
    {
       return artistName; // Sends ipodModel to calling method.
    }
    public String getSongTitle() // Method that returns the ipodColor.
    {
       return songTitle; // Sends ipodColor to calling method.
    }
    public String getAlbumTitle() // Method that returns the ipodColor.
    {
       return albumTitle; // Sends ipodColor to calling method.
    }
    
    public int getSongRating() // Method that returns the yearPurchased.
    {
       return songRating; // Sends yearPurchased to calling method.
    }
    
    public int getNumberOfSongs() // Method that returns the dataStorage.
    {
       return numberOfSongs; // Sends dataStorage to calling method.
    }
    
    public int compareTo(Object o)
    	{
    	   return toString().compareTo(o.toString());
    	}
    // This is a special toString() method that returns the attributes of a Ipod object
    	// when the object used in a String context (e.g., System.out.println(ipodObject);). 
    public String toString() 
    		{
    			return "Artist Name: "+getArtistName()+"\n"+ // returns ipodModel.
    				"Song Title: "+getSongTitle()+"\n"+ // returns ipodColor.
    					"Album Title: "+getAlbumTitle()+"\n"+ // returns yearPurchased.
    					  "Song Rating(stars): "+getSongRating()+"\n"+ // returns yearPurchased.
    						 "Number of Songs in Playlist: "+getNumberOfSongs(); // returns dataStorage.
    		
       
     }
    }
    Last edited by offfbyheart; 05-04-2011 at 10:14 PM.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    One tip I'd like to suggest, in the actual class you do not need to use setters to modify the values of the classes instance variables. The constructor and toString and other methods can change and manipulate the variables directly.

    What exactly is your current error? And which code produces it?

  12. #12
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Both current errors seem to be in the PlayListBuilder and PlayListBuilderArray codes. They each have the same exact problem and that is one or the other. Problem one is that When I run the program and the results are shown for the lists this shows up:

    Java Code:
    Number of Songs in Playlist: 0
    I don't understand how to fix this, and I'm not sure if it is necessary since it is already give at the end of the list which is what I like the most.

    Basically, I want to get rid of the 'Number of Songs in Playlist: 0"

    and keep: "The number of songs in this playlist are: 1." result.

    soo itll look like this:

    Java Code:
    Song #1:
    Artist Name: jade
    Song Title: portia
    Album Title: wanda
    Song Rating(stars): 5
    
    --------------------------------------
    The number of songs in this playlist are:  1.

  13. #13
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    No matter what I enter, the number of songs playlist wont seem to ever work. or disappear.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    My phone cuts that line off would you mind posting just the line that prints the total?

  15. #15
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
     for (int count = 0; count < numberOfSongs; count++)
    		{
    		   System.out.println("Song #"+(count+1)+":");
    			System.out.println(songList[count]);
    			System.out.println("--------------------------------------");
    			sum = sum + songList[count].getNumberOfSongs();
    		}
    		
    		System.out.println("The number of songs in this playlist are:  "+(numberOfSongs)+".");
    	}
    }

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Still cuts it off :( just show the last print line.

  17. #17
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Java Code:
    System.out.println("The number of songs in this playlist are:  "+(numberOfSongs)+".");

  18. #18
    offfbyheart is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    I think that one is the one that works, its the other one that doesnt.

  19. #19
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    If all you want to do is to get rid of is "Number of Songs in Playlist: 0", then remove that line in Playlist.toString(). Does it work otherwise (i.e. you get the list of songs in the playlist)?

  20. #20
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Also, you never set the song rating nor song number in your loop. You read the rating, but never use the read string.

    EDIT: Or at least you didn't set the rating before the second code-block. I still can't find you setting the song number.

Page 1 of 2 12 LastLast

Similar Threads

  1. Array code problem
    By raladin3d in forum New To Java
    Replies: 4
    Last Post: 04-11-2011, 06:50 AM
  2. Array Code difficulties
    By NixasMuraki in forum New To Java
    Replies: 2
    Last Post: 02-08-2011, 12:17 AM
  3. code using array
    By java008 in forum New To Java
    Replies: 11
    Last Post: 12-18-2008, 10:44 AM
  4. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 07:42 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
  •