0

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?

3
  • you have todo something like this geeksforgeeks.org/switch-statement-in-java Commented Dec 30, 2019 at 20:06
  • @RamPrakash yes something like this but instead of single statement, I have to print out the entire array, and one SPECIFIC element from a specific index of the array. Commented Dec 30, 2019 at 20:10
  • have alook at developer.com/java/data/… Commented Dec 30, 2019 at 20:18

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.