Home > Back-end >  Syntax error on token "{", SwitchLabels expected after this token-switch (char)
Syntax error on token "{", SwitchLabels expected after this token-switch (char)

Time:09-17

I'm banging my head against the wall.... I'm new...very new to this. I'm receiving an error "Syntax error on token "{", SwitchLabels expected after this token" at the bolded line - switch (operator) {. Every time I change it something else fails.

import java.util.Scanner;

public class Assignment2 {
    
public static void main(String[] args) {
        // Prompt for 2 numbers and a symbol
        
    Scanner scan = new Scanner(System.in);
    
    //Prompt for input
    System.out.println("Enter a number: ");
    double num1 = scan.nextDouble();
    System.out.println("Enter a number: ");
    double num2 = scan.nextDouble();
    System.out.println("Enter   - * or /");
    char operator = scan.next().charAt(0);
    
    switch (operator) {
    
    /*previous attempt
    //if (operator == " ") 
    //  System.out.println(num1   " "   num2   "="   (num1 num2));
    // else if (operator == "-")
    //  System.out.println(num1 - num2);
    // else if (operator == "*")
    //  System.out.println(num1 * num2);
    // else if (operator == "/")
    //  System.out.println(num1 / num2); */
    
    double answer;
    
    case " ":
        answer = num1   num2;
        System.out.println(num1   " "  num2   "="   answer);
        break;
    
    case "-":
        answer = num1 - num2;
        System.out.println(num1   "-"  num2   "="   answer);
        break;
    
    case "*":
        answer = num1 * num2;
        System.out.println(num1   "*"  num2   "="   answer);
        break;
    
    case "/":
        answer = num1 / num2;
        System.out.println(num1   "*"  num2   "="   answer);
        break;
        
    
    //reject all others
    default:
        System.out.println("Error: Not a valid symbol!");
        break;
    }
    
    scan.close();           
  }

}

CodePudding user response:

You cant define variables there, put the line double answer; before switch (operator) {

CodePudding user response:

You were reading the operator as char and in the switch case you were using them as string. The below code should work for you.

import java.util.Scanner;

public class Assignment2 {

public static void main(String[] args) { // Prompt for 2 numbers and a symbol

    Scanner scan = new Scanner(System.in);
    

    //Prompt for input
    System.out.println("Enter a number: ");
    double num1 = scan.nextDouble();
    System.out.println("Enter a number: ");
    double num2 = scan.nextDouble();
    System.out.println("Enter   - * or /");
    char operator = scan.next().charAt(0);
        double answer;
    switch (operator) {
    
    /*previous attempt
    //if (operator == " ") 
    //  System.out.println(num1   " "   num2   "="   (num1 num2));
    // else if (operator == "-")
    //  System.out.println(num1 - num2);
    // else if (operator == "*")
    //  System.out.println(num1 * num2);
    // else if (operator == "/")
    //  System.out.println(num1 / num2); */
    

    
    case ' ':
        answer = num1   num2;
        System.out.println(num1   " "  num2   "="   answer);
        break;
    
    case '-':
        answer = num1 - num2;
        System.out.println(num1   "-"  num2   "="   answer);
        break;
    
    case '*':
        answer = num1 * num2;
        System.out.println(num1   "*"  num2   "="   answer);
        break;
    
    case '/':
        answer = num1 / num2;
        System.out.println(num1   "*"  num2   "="   answer);
        break;
        
    
    //reject all others
    default:
        System.out.println("Error: Not a valid symbol!");
        break;
    }
    
    scan.close();
            
}
}

CodePudding user response:

try this



import java.util.Scanner;

public class Assignment2 {

    public static void main(String[] args) { // Prompt for 2 numbers and a symbol

        Scanner scan = new Scanner(System.in);


        //Prompt for input
        System.out.println("Enter a number: ");
        double num1 = scan.nextDouble();
        System.out.println("Enter a number: ");
        double num2 = scan.nextDouble();
        System.out.println("Enter   - * or /");
        char operator = scan.next().charAt(0);


        double answer;
        switch (operator) {
            case ' ':
                answer = num1   num2;
                System.out.println(num1   " "   num2   "="   answer);
                break;

            case '-':
                answer = num1 - num2;
                System.out.println(num1   "-"   num2   "="   answer);
                break;

            case '*':
                answer = num1 * num2;
                System.out.println(num1   "*"   num2   "="   answer);
                break;

            case '/':
                answer = num1 / num2;
                System.out.println(num1   "*"   num2   "="   answer);
                break;


            //reject all others
            default:
                System.out.println("Error: Not a valid symbol!");
                break;
        }

        scan.close();

    }
}
  • Related