Reputation: 19
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
Upvotes: 0
Views: 62
Reputation: 62854
For each case
in the switch
, you have to add a break;
statement, otherwise a situation, called fall-through† will happen:
switch(choice) {
case 1:
setSongTitle();
menu();
break;
case 2:
duration();
menu();
break;
case 3:
searchRemove();
break;
default:System.out.println("Sorry try again");
menu();
}
† Fall through
The
break
statements are necessary because without them, statements inswitch
blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
Upvotes: 4
Reputation: 17880
1)The problem is that you are not breaking when a match is found. And every time a mismatch is found else gets executed
Change like this
int flag=0;
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;
flag=1; //set a flag to denote that a match is found
break;
}//if
}//for
if(flag==0)
System.out.println("Sorry your search has not been found");
2) And you must add break
after each case
in switch
Upvotes: 0
Reputation: 1356
You need to add breaks
into your switch statement before the next case
. Example:
switch(choice)
{
case 1:
setSongTitle();
menu();
break;
case 2:
duration();
menu();
break;
case 3:
searchRemove();
break;
default:System.out.println("Sorry try again");
menu();
}
Upvotes: 1