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
-----------------------------------------
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.
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.
}
}