Results 1 to 20 of 26
- 05-04-2011, 07:22 PM #1
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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
-----------------------------------------
- 05-04-2011, 07:33 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Please use code tags.
[code]
YOUR CODE HERE
[/code]
- 05-04-2011, 07:48 PM #3
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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("-----------------------------------------"); } } }
- 05-04-2011, 07:50 PM #4
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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)+"."); } }
- 05-04-2011, 09:15 PM #5
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
anyone? :(
- 05-04-2011, 09:32 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If no one has answered by then I will be able to give you advice in about 2 hours.
- 05-04-2011, 09:33 PM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
I will give you an answer in about 45 minutes.
- 05-04-2011, 09:59 PM #8
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Okay. Thank you so much. I really appreciate this!
- 05-04-2011, 10:09 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Where is the playlist class?
- 05-04-2011, 10:12 PM #10
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 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.
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.
- 05-04-2011, 10:19 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 05-04-2011, 10:27 PM #12
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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:
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.Java Code:Number of Songs in Playlist: 0
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.
- 05-04-2011, 11:37 PM #13
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
No matter what I enter, the number of songs playlist wont seem to ever work. or disappear.
- 05-04-2011, 11:38 PM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
My phone cuts that line off would you mind posting just the line that prints the total?
- 05-04-2011, 11:39 PM #15
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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)+"."); } }
- 05-04-2011, 11:44 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Still cuts it off :( just show the last print line.
- 05-04-2011, 11:47 PM #17
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
Java Code:System.out.println("The number of songs in this playlist are: "+(numberOfSongs)+".");
- 05-04-2011, 11:51 PM #18
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
I think that one is the one that works, its the other one that doesnt.
- 05-05-2011, 12:11 AM #19
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)?
- 05-05-2011, 12:21 AM #20
Similar Threads
-
Array code problem
By raladin3d in forum New To JavaReplies: 4Last Post: 04-11-2011, 06:50 AM -
Array Code difficulties
By NixasMuraki in forum New To JavaReplies: 2Last Post: 02-08-2011, 12:17 AM -
code using array
By java008 in forum New To JavaReplies: 11Last Post: 12-18-2008, 10:44 AM -
please i need the code of comparing these two array lists.
By raj reddy in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 04-18-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks