Reputation: 111
My code:
import javax.swing.JOptionPane;
public class IT106_Playlist {
public static void main(String[] args){
final int MAX_SONGS = 106;
int totalDuration = 0;
int numSongs=0;
boolean exitVar=false;
int i=0;
String[] songTitles = new String[MAX_SONGS];
int[] songLengths = new int[MAX_SONGS];
while(exitVar==false && numSongs<=songTitles.length) {
if (songTitles.equals("-1")){
exitVar=true;
}
do{
songTitles[numSongs] = JOptionPane.showInputDialog(null, "Enter a song name, or type -1 to exit");
if (!songTitles.equals("")){
JOptionPane.showMessageDialog(null, "Error: Please enter a valid song name, or type -1 to exit");
}else{
++numSongs;
}
}while(!songTitles[numSongs].equals(""));
do{
try{
songLengths[numSongs] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a song length, e.g. 4, or type -1 to exit"));
}catch (NumberFormatException e){
songLengths[numSongs]=-1;
JOptionPane.showMessageDialog(null, "Error: please enter a valid song length, e.g. 4, or type -1 to exit");
}
totalDuration+=songLengths[numSongs];
/* line 41 */ }while(songLengths[numSongs]=-1);
boolean addMore=true;
while((numSongs<=MAX_SONGS) && (addMore=true)){
JOptionPane.showMessageDialog(null, "Song #" + i + ": "+ songTitles[i] + " length: " + songLengths[i] + "\n");
i++;
if(songTitles[i] ==null){
addMore=false;
}
}
}
}
}
Hi. I am trying to figure out why I have an error on line 41 of my code saying int cannot be converted to boolean, and I wanted to know why/how to fix this.
Basically, I am making a playlist. The user enters the song name and length separately(two different arrays) and then once they either reach the max amount of songs, or they wish to exit, they can do so by typing -1.
Then, the user can remove songs from their playlist, and then after that they can exit, printing all of the songs and the total duration, as well as the total number of songs.
We cannot use arrayLists, or any methods.
My question is, 1. is my setup okay, and 2. how do I make the songs print after each song name has been entered? I am so close to figuring this out..Thank you!
Upvotes: 1
Views: 67
Reputation: 1205
Caution, there are two problems here. One of them is the one showing an error involving the song lengths, this is what it should be:
while (songLengths[numSongs] == -1);
You also have a hidden bug a couple lines down, where you run while ((numSongs <= MAX_SONGS) && (addMore = true))
. This is assigning addMore
to true
, you want to run (addMore == true)
to compare.
EDIT Just found the error dealing with line 30. You are checking if an String[]
equals a ""
like so: if (!songTitles.equals("")) {
. This is incorrect, and you also are !
ing it, you want to check if (songTitles[numSongs].equals("")) {
.
EDIT 2 Turns out that wasn't the entire problem with line 30. In your do while
, at that line, you have A) A !
where you don't want it, and B) if the user does enter in a proper song name, previously in the do while
, you incremented numSongs
, now you need to get the previous one to compare like so:
} while (songTitles[numSongs-1].equals(""));
EDIT 3 & 4 I've now fully read the code, and here is what I think you are looking for. I've put in comments explaining some of the main changes. Also, now handling the no System.exit(0);
usage.
import javax.swing.JOptionPane;
public class IT106_Playlist {
public static void main(String[] args) {
final int MAX_SONGS = 106;
int totalDuration = 0;
int numSongs = 0;
boolean exitVar = false;
int i = 0;
String[] songTitles = new String[MAX_SONGS];
int[] songLengths = new int[MAX_SONGS];
while (numSongs <= songTitles.length) {
// Removed your check to see if the song name is -1, and placed it later on
do {
songTitles[numSongs] = JOptionPane.showInputDialog(null,
"Enter a song name, or type -1 to exit");
if (songTitles[numSongs].equals("")) {
JOptionPane.showMessageDialog(null,"Error: Please enter a valid song name, or type -1 to exit");
} else if (songTitles[numSongs].equals("-1")) { // Checks if -1
exitVar = true;
System.exit(0); // This is how to quickly exit your program safely
}
} while (songTitles[numSongs].equals("")); // Keep asking the user for input if they don't provide any
if (exitVar) {
break;
}
do {
try {
songLengths[numSongs] = Integer
.parseInt(JOptionPane
.showInputDialog(null,
"Enter a song length, e.g. 4."));
if (songLengths[numSongs] > 0) { // Check if the length is more than 0 (assuming no songs are 0 in length)
totalDuration += songLengths[numSongs]; // Add to totalDuration only if it is a valid length
} else { // If song length is invalid, display error
songLengths[numSongs] = -1;
JOptionPane
.showMessageDialog(null,
"Error: please enter a valid song length, e.g. 4.");
}
} catch (NumberFormatException e) { // Catch if could not parse int
songLengths[numSongs] = -1;
JOptionPane
.showMessageDialog(null,
"Error: please enter a valid song length, e.g. 4.");
}
} while (songLengths[numSongs] <= 0); // Only do this if you don't have a valid song length
boolean addMore = true;
while ((numSongs <= MAX_SONGS) && (addMore == true)) { // addMore bug fixed
// This is my addition, but to be more user friendly, I put in (i+1), so that the user doesn't question what "Song #0" is
JOptionPane.showMessageDialog(null, "Song #" + (i+1) + ": "
+ songTitles[i] + " length: " + songLengths[i] + "\n");
i++;
if (songTitles[i] == null) {
addMore = false;
}
}
numSongs++; // Moved this way down here to avoid messing code up above
}
}
}
Upvotes: 2
Reputation: 7304
Is
}while(songLengths[numSongs]=-1);
perhaps supposed to be
}while(songLengths[numSongs]==-1);
The former is always assigning the value -1
to songLengths[numSongs]
, which results in an integer value and in Java this won't convert to a boolean.
You also probably meant while((numSongs<=MAX_SONGS) && addmore)
.
Upvotes: 0
Reputation: 3896
Looks like this line is the culprit:
while(songLengths[numSongs]=-1);
What you seem to want there should actually be:
while(songLengths[numSongs] == -1);
A single =
means to assign a value; a double ==
is for comparing values.
Upvotes: 0