Reputation: 119
I am practicing switch statement in java, and I have came across this example in java where I am trying to write a program which will have name of an artist and list of 17 songs of that artist. In one case I have to print out the 16th element of the array and another case I have to print out the last element of the array and by default it will print out everything (artist name and 17 songs). So, far I have declared the array:
public class q7 {
public static void main(String[] args) {
//String[] artist = {"Tahsan"};
//declaring an array that contains 18 elements in total, which includes the name of the artist and 17 tracks
String[] songs = {"Tahsan", "Alo","Irsha", "Odrissho Robi","Prematal", "Aalo", "Nei", "Rodela Dupur","Ke Tumi",
"Alo","Brittalpona","durotto","Brishtite","Durey","Bhalobashar Maane","Tomay Ghire","Kothopokhoton", "Prottaborton"
};
for (String i : songs){
System.out.println(i);
}
switch(){
}
}
How should I start writing the switch statement?
Upvotes: 0
Views: 482
Reputation: 330
Since it was not given on what parameter switch case needs to work I have taken input from the user and added the following case asked. For more information regarding switch case, you can refer to switch case in Java GFG
import java.util.Scanner;
public class q7 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//String[] artist = {"Tahsan"};
//declaring an array that contains 18 elements in total, which includes the name of the artist and 17 tracks
String[] songs = {"Tahsan", "Alo","Irsha", "Odrissho Robi","Prematal", "Aalo", "Nei", "Rodela Dupur","Ke Tumi",
"Alo","Brittalpona","durotto","Brishtite","Durey","Bhalobashar Maane","Tomay Ghire","Kothopokhoton", "Prottaborton"
};
int testCase = keyboard.nextInt();
switch(testCase){
case 1: System.out.println(songs[16]);
break;
case 2: System.out.println(songs[songs.length-1]);
break;
default : for (String i : songs)
System.out.println(i);
}
}
}
Hope this helps you!
Upvotes: 1
Reputation: 34
You should define switch case statements.
//declaring an array that contains 18 elements in total, which includes the name of the artist and 17 tracks
String[] songs = {"Tahsan", "Alo","Irsha", "Odrissho Robi","Prematal", "Aalo", "Nei", "Rodela Dupur","Ke Tumi",
"Alo","Brittalpona","durotto","Brishtite","Durey","Bhalobashar Maane","Tomay Ghire","Kothopokhoton", "Prottaborton"
};
for (String i : songs){
System.out.println(i);
}
int type=0;
switch(type){
case 0:
System.out.println(songs[16]);
break;
case 1:
System.out.println(songs[songs.length()-1]);
break;
default:
for (String i : songs){
System.out.println(i);
}
}
You should change type value. It is better if you take it from user.
Upvotes: 0
Reputation: 693
A switch statement is fairly similar to an if statement, where each 'case' is a condition for some object/value.
Therefor, if you had an index for your array that was inputted by something else (e.g. a user selected it), it would look like this:
switch(songs[index]){
case "Tahsan": System.out.println(songs[index]);
break;
case "Alo": System.out.println(songs[index]);
break;
//etc etc
default:
for (String i : songs){
System.out.println(i);
}
}
you could also use the index itself as the conditional value.
switch(index){
case 1: System.out.println(songs[1]);
break;
case 2: System.out.println(songs[2]);
break;
default:
for (String i : songs){
System.out.println(i);
}
}
etc.
It doesn't make much practical sense if you don't have the index being chosen by some other system (some other application or a user for example), but that is the gist of how it works.
Upvotes: 0