user1714873
user1714873

Reputation: 135

Switch statement in 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);
    }
}

Upvotes: 4

Views: 761

Answers (5)

Boring Useless Guy
Boring Useless Guy

Reputation: 1

Using strings in switch case can only be used if you using JDK7 and even then you will have to have the values in quotes. Like

case "a":

Upvotes: 0

PermGenError
PermGenError

Reputation: 46408

your case should be a char like case 'a'

 switch(character) 

 {
    case 'a':
       //do your stuff
    }

and also you are declaring leet(String variable twice). just declare it one and use the same variable when you get input from the scanner

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15644

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

Upvotes: 3

kosa
kosa

Reputation: 66637

case statement using char (which means single quote), it should be something like

 switch (character)
            {
                case 'a': Leet = "4";
                    break;
                case 'b': Leet = "I3";
                    break;
             .........
            }

Upvotes: 1

AlexR
AlexR

Reputation: 115328

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.

Upvotes: 5

Related Questions