Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement in Java

Tags:

java

Basically I need to take a letter A-Z and convert it to Leek(a combo of sign,#,letter that look like the A-Z characters. I'm only allow to use switch statements (switch,case,breaks) also I have to use the .next().charAt(0) method.

When I try to compile my program it comes up with multiple error all reading "can not find symbol" pointing at the a-z character I used in the case statement.

import java.util.Scanner;

public class dlin_Leet
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        char character;//input by user
        String Leet;

        System.out.print("Enter character to convert:");
        String Leet = input.next();
        char character = Leet.charAt(0);

        switch (character)
        {
            case a: Leet = "4";
                break;
            case b: Leet = "I3";
                break;
            case c: Leet = "[";
                break;
            case d: Leet = ")";
                break;
            case e: Leet = "3";
                break;
            case f: Leet = "|=";
                break;
            case g: Leet = "&";
                break;
            case h: Leet = "#";
                break;
            case i: Leet = "1";
                break;
            case j: Leet = "J";
                break;
            case k: Leet = "|<";
                break;
            case l: Leet = "1";

             }
        System.out.println(Leet);
    }
}
like image 799
user1714873 Avatar asked Jan 15 '23 10:01

user1714873


2 Answers

The character constants must be in into apostraphs:

case 'a': instead of case a:

Fix your code and I hope this is the only syntax error you have.

like image 133
AlexR Avatar answered Jan 25 '23 15:01

AlexR


Also - You are declaring variable "Leet" and "character" twice in the same block( Duplicate local variable)

like image 41
Abubakkar Avatar answered Jan 25 '23 15:01

Abubakkar