mercredi 25 février 2015

If statement prints out the 'else' part even if it's correct

I have designed some code that will ask the user for 10 song titles(which will be stored in an array), then the duration of those songs (also in an array) and finally they can search and delete a song from their list.


However I when I run the search and remove section, the program runs my whole if statement. I have an 'else' so that if they search a song that isn't in the list , it will say 'sorry' and return them to the start. But it continues to run that even if they do search a correct song title. Can someone help?



import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
* Date: 25/02/2015
* Time: 13:37
* UPDATE COMMENT ABOUT PROGRAM HERE
*/
public class Songs
{
static final int SIZE=10;
static String songTitle [] = new String[SIZE];
static Scanner keyboard = new Scanner(System.in);
static double duration[]=new double[SIZE];
static int choice;
static String searchSong;

public static void menu()
{
System.out.println("Please chose from the options below");
System.out.println("1) Enter Song Titles **DO FIRST**");
System.out.println("2) Enter duration of songs **DO SECOND**");
System.out.println("3) Search and remove **DO THIRD**");
choice=keyboard.nextInt();

switch(choice)
{

case 1:
setSongTitle();
menu();

case 2:
duration();
menu();

case 3:
searchRemove();

default:System.out.println("Sorry try again");
menu();

}//switch
}//menu

public static void setSongTitle()
{
for(int count=0;count<SIZE;count++)
{

System.out.println("Please enter your song title number " + (count + 1) + " below..");
songTitle[count]=keyboard.next();

}//setSongTitle();

System.out.println("Here are your songs");

for(int count=0;count<SIZE;count++)
{
System.out.println(songTitle[count]);
}//for



}//setSongTitle

public static void duration()
{
for(int count=0;count<SIZE;count++)
{

System.out.println("Please enter the duration of the song " +songTitle[count] + " below...");
duration[count]=keyboard.nextDouble();

}//for

for(int count=0;count<SIZE;count++)
{
System.out.println(duration[count]);
}//for

}//duration

public static void searchRemove()
{

System.out.println("Please enter a song title you would like to remove");
searchSong=keyboard.next();

for(int count=0;count<SIZE;count++)
{




if(searchSong.equals(songTitle[count]))
{

System.out.println("Song " +songTitle[count] + " is now removed from the list");
System.out.println("The duration of " + duration[count] + " for song " +songTitle[count] + " is now removed from the list");
duration[count]=0;
songTitle[count]=null;

}//if

else
{
System.out.println("Sorry your search has not been found");
searchRemove();
}//else

}//for


}

public static void main(String[] args)
{
menu();
}//main
}//class

Aucun commentaire:

Enregistrer un commentaire