Question

I'm kinda new to this but? can anyone correct my first switch statement? I dont know what expression to use to start the case a:

and im not even sure if the starting declarations are even correct

    import java.io.*;
    public class SwitchDemo {
    public static void main(String[] args) {


    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input= br.readLine();

    System.out.println("Press Enter");
    System.out.println("a.Hello: Asks the name of the user.");
    System.out.println("b.Array: Input elements and search for a specified key and prints a     message.");
    System.out.println("c.MagicSquare: Displays [7][7] magic square.");
    System.out.println("d.Bubble Sort: Alphabetically sorts 20 employees using bubble sort.");
    System.out.println("e.Selection Sort: Alphabetically sorts 20 employees using selection sort");
    System.out.println("f.Insertion Sort: Alphabetically sorts 20 employees using Insertion sort");
    System.out.println("g.Factorial: Run the Factorial application");
    System.out.println("h.Triangle: Run the Triangle application."); 
    System.out.println("i.MergeSort: Performs the mergesort of a two class databasse.");
    System.out.println("j.Stack_1: Perform reversal of string.");
    System.out.println("k.Stack_2: Perform Infix Notation");
    System.out.println("l.Postfix: Perform Postfix Notation");
    System.out.println("m.Linked List");
   System.out.println("n.Queue:");
   System.out.println("o.Exit:");

        switch (input) {
            case a:

            String usersName;     
           String upperCaseName;  

           TextIO.put("Please enter your name: ");
           usersName = TextIO.getln();

           upperCaseName = usersName.toUpperCase();

           TextIO.putln("Hello, " + upperCaseName + ", nice to meet you!");


                     break;
            case b: 
        // Code for b execution here. Run array.
                     break;
            case c:  
        // Code for c execution here. magicsquare.
                     break;
            case d: 
        // Code for d execution here. Bubble sort.
                     break;
            case e:  
        // Code for e execution here. selection sort.
                     break;
            case f: 
        // Code for f execution here. insertion sort.
                     break;
            case g:  
        // Code for g execution here. recursion.
                     break;
            case h:  
        // Code for h execution here. mergesort.
                     break;
            case i:  
        // Code for b execution here. stack1.
                     break;
            case j: 
        // Code for b execution here. stack2.
                     break;
            case k: 
        // Code for b execution here. link list.
                     break;
            default: 
        System.out.println("Please input selection from a-o");
                     break;
        }

    }
}
Was it helpful?

Solution

As others have mentioned, using

switch(input) {
    case "a":
        // do stuff
        break;
    case "b":
        // do more stuff
        break;
}

will work. Just another thing, if you are working in java 7 (java -version), you should be able to get rid of

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

and replace it with

Scanner in = new Scanner(System.in);
String input = in.nextLine();

Which at least to me is a lot easier to use. Just import

import java.util.Scanner;

OTHER TIPS

A couple of points here

case statements are like this (pre 1.7)

char input = 'a';

switch (input) {
case 'a':
    break;
default :
    System.out.println("default");

}

for Java 1.7 and onwards you can also switch on Strings

String input = "a";

switch (input) {
case "a":
    printHelloUser();
    break;
default :
    System.out.println("default");

}

void printHelloUser () {
       String usersName;     
       String upperCaseName;  

       TextIO.put("Please enter your name: ");
       usersName = TextIO.getln();

       upperCaseName = usersName.toUpperCase();

       TextIO.putln("Hello, " + upperCaseName + ", nice to meet you!");


}

you may also want to consider prompting the user first before reading her input.

You aren't checking the cases as Strings (you declare input as a String). You are calling variables called a, b, etc. Try putting them into quotation marks, as in case: "a".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top