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);
}
}
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.
Also
- You are declaring variable "Leet" and "character" twice in the same block( Duplicate local variable)
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;
.........
}
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
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":
Related
public class Conditionsif {
public static void main(String[] args) {
// TODO Auto-generated method stub
int day=1;
switch(day){
case '1':
System.out.println("Monday");
break;
}
}
}
There is no compilation error in above though switch expression is integer data type and case value is character
Its because of implicit casting
ascii value of a is 97
switch(97){
case 'a': System.out.println("a"); break;
case 'b': System.out.println("b"); break;
case 'c': System.out.println("c"); break;
}
When you use ' ', this means you use char data type.
char data type accept one character only ,
for example :
char x = 'h';
And also accept the Ascii code for the character
for example :
char x = 104;
So case '1' mean compare variable day with value equal to value 1.
if you modify code as following , this code will print Monaday.
int day=97;
switch(day){
case 'a':
System.out.println("Monday");
break;
}
Best Regards.
This question already has answers here:
In a switch statement, why are all the cases being executed?
(8 answers)
Closed last year.
I am working on the game where the columns of the board are represented by the characters but i would like to assign them an index.
I have decided to use the switch statement in that case, however it does produce the wrong result.
With the current code I attach, it gives me 14 as an index, however since the string is 7h, and it takes h as a char, it should give an index of 7. What Could be an issue? Thanks in advance!
public class Check {
public int columnToInt(char c) {
int index=0;
switch(c) {
case 'a':
index=0;
case 'b':
index=1;
case 'c':
index=2;
case 'd':
index=3;
case 'e':
index=4;
case 'f':
index=5;
case 'g':
index=6;
case 'h':
index=7;
case 'i':
index=8;
case 'j':
index=9;
case 'k':
index=10;
case 'l':
index=11;
case 'm':
index=12;
case 'n':
index=13;
case 'o':
index=14;
}
return index;
}
public static void main(String[] args) {
String myStr = "7h";
char c =myStr.charAt(1);
System.out.println("the char at position 1 is "+c);
Check check = new Check();
int result = check.columnToInt(c);
System.out.println(result);
}
}
Java switch statements can be a bit annoying to use. You need to use break or all the cases after the expected one will be executed as well.
switch(c) {
case 'a':
index=0;
break;
Alternatively you can use a return.
switch(c) {
case 'a':
return 0;
You must add the break keyword for each case.
For example:
case 'a':
index=0;
break;
otherwise next assignments are applied.
why cant i convert String to char & use in Switch Statement & if i left it as string the switch statement wont accept it either telling me it needs to be int or byte or short !!
public class Main {
public static void main(String[] args) {
String var1=getInput("enter first variable");
String var2=getInput("enter second variable");
String var3=getInput("enter opertaor");
char c = var3.charAt(0);
double d1=Double.parseDouble(var1);
double d2=Double.parseDouble(var2);
switch(c){
case "+"://squiggly line appears & the bubble help says incompatible types
System.out.println(d1+d2);
break;
case "-"://squiggly line appears & the bubble help says incompatible
System.out.println(d1-d2);
break;
case "*"://squiggly line appears & the bubble help says incompatible
System.out.println(d1*d2);
break;
case "/"://squiggly line appears & the bubble help says incompatible
System.out.println(d1/d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
}
static String getInput(String prompt){
System.out.println("prompt");
Scanner sc=new Scanner(System.in);
return sc.nextLine();
}
}
You can use a String in case expressions, no need for a char. Change c like
String c = var3.substring(0, 1);
and your code would work. Alternatively, modify your case statements to use char. Like,
switch (c) {
case '+':
System.out.println(d1 + d2);
break;
case '-':
System.out.println(d1 - d2);
break;
case '*':
System.out.println(d1 * d2);
break;
case '/':
System.out.println(d1 / d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
You can use a char literal instead:
switch(c){
case '+':
System.out.println(d1+d2);
break;
...
The types are different so you can't directly compare them. They happen to be convertible in this particular case, but that's not true in general, so the compiler can't allow that.
Try out below code and it will execute
public static void main(String[] args) {
String var1 = getInput("enter first variable");
String var2 = getInput("enter second variable");
String var3 = getInput("enter opertaor");
char c = var3.charAt(0);
double d1 = Double.parseDouble(var1);
double d2 = Double.parseDouble(var2);
switch (c) {
case '+':
System.out.println(d1 + d2);
break;
case '-':
System.out.println(d1 - d2);
break;
case '*':
System.out.println(d1 * d2);
break;
case '/':
System.out.println(d1 / d2);
break;
default:
System.out.println("Unrecognized operation");
break;
}
}
static String getInput(String prompt) {
System.out.println("prompt");
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
Change case '+': instead of comparing String case "+": Also Check your Java version, From JDK v1.7 will allow you to use Strings in switch statement as what you did in your code snippet. Let me know if you are looking for another solution
You cant parse a String into a char because a String is to big to fit.
Think of it like a String consist of multiple chars, so its just like a char array in one piece.
I am trying to write a program that will switch any letter of the alphabet (upper or lower cases) AND number into the Phonetic alphabet. For example, if I enter "A" or "a" my program will give me (change it to) "Alpha". Moreover, if I enter "1" it will return "One". I've successfully managed to work the 'enter-any-letter' aspect of it, but my program does not recognize numbers. I tried putting int but my Scanner does not recognize this. I put a default in my code but still...no prevail. Should I use an if statement instead?
Further note:
This is question is a continuation from this question
Here's what I've got so far:
import java.util.Scanner;
public class PhoneticTranslate {
public static void main(String[] args) {
int number = 0;
char letter;
String phonetic = null;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter or number: ");
letter = kb.next().charAt(0);
switch(Character.toUpperCase(letter))
{
case 'A':
phonetic = "Alpha";
break;
case 'B':
phonetic = "Bravo";
break;
// ... rest of cases for letters
case 'Z':
phonetic = "Zulu";
break;
default:
Scanner x = new Scanner(System.in);
number = kb.nextInt();
switch(number)
{
case '1':
phonetic = "One";
break;
case '2':
phonetic = "Two";
break;
// ... rest of cases for numbers
case '8':
phonetic = "Eight";
break;
case '9':
phonetic = "Nine";
break;
}
}
System.out.println("You Entered " + letter + ". This letter indicates: " + phonetic);
System.out.println("You Entered" + number + ". This number indicates: " + phonetic);
}
}
A giant switch/case clause is a code smell, try this:
Add every key/value pair into a Map, then you retrieve the values with get. No switch/case needed.
String letter;
String phonetic;
Map<String,String> codes = new HashMap<String,String>();
codes.put("A","Alpha");
codes.put("B","Bravo");
codes.put("C","Charlie");
codes.put("D","Delta");
// not showing all "puts" to make it shorter
codes.put("W","Whiskey");
codes.put("X","X-Ray");
codes.put("Y","Yankee");
codes.put("Z","Zulu");
codes.put("0","Zero");
codes.put("1","One");
// not showing all "puts" to make it shorter
codes.put("9","Nine");
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = kb.next().toUpperCase(); // convert key to uppercase
phonetic = codes.get(letter); // search the value in the map using the key
if (phonetic == null) {
System.out.println("bad code : " + letter);
} else {
System.out.println("Phonetic: " + phonetic);
}
You have written your cases over characters: -
case '1': // This is checking for character '1'
You need to change your cases to take integer values: -
switch(number) {
case 1:
phonetic = "One";
break;
case 2:
... so on
Either don't include the quotes around the number (" case 1: phonetic = "One"" etc), or continue using the char value. I think either one should work.
Your switch statement is checking for the unicode char representation of integers. By this specification, '1' is the character "1" which translates to the integer 49.
Put the int representation of each value in the switch statement:
switch (number) {
case 1:
phonetic = "One";
break;
case 2:
...
}
Try:
Scanner x = new Scanner(System.in);
int number = x.nextInt();
String phonetic = null;
switch(number)
{
case 1:
phonetic = "One";
break;
case 2:
phonetic = "Two";
break;
case 3:
phonetic = "Three";
break;
case 4:
phonetic = "Four";
break;
case 5:
phonetic = "Five";
break;
case 6:
phonetic = "Six";
break;
case 7:
phonetic = "Seven";
break;
case 8:
phonetic = "Eight";
break;
case 9:
phonetic = "Nine";
break;
}
Use the ASCII codes for the numbers instead , that's what chars are anyway. But why do you need to do it? Doesnt your code already work?
Continue your cases into the characters representing integers:
case 'Z':
phonetic = "Zulu";
break;
case '1':
phonetic = "One";
break;
case '2':
// ...
This will work, so long as you only want to handle single-digit numbers.
This matches your problem description, though keeping both a letter and number variable and printing them out separately suggests some additional functionality?
You can try to check if the input value is a digit(int). If not return
while(true){
Scanner x = new Scanner(System.in);
int number=0;
try{
int number = x.nextInt();
}catch(IllegalArgumentException e){
continue;
}
String phonetic = null;
switch(number)
{
case 1:
phonetic = "One";
break;
case 2:
phonetic = "Two";
break;
case 3:
phonetic = "Three";
break;
case 4:
phonetic = "Four";
break;
case 5:
phonetic = "Five";
break;
case 6:
phonetic = "Six";
break;
case 7:
phonetic = "Seven";
break;
case 8:
phonetic = "Eight";
break;
case 9:
phonetic = "Nine";
break;
}
}
Try this one sir
package phone;
import java.util.Scanner;
public class PhoneticTranslate {
/**
* #param args
*/
public static void main(String[] args) {
int number = 0;
char letter;
String phonetic = null;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter or number: ");
letter = kb.next().charAt(0);
switch (Character.toUpperCase(letter)) {
case 'A':
phonetic = "Alpha";
break;
case 'B':
phonetic = "Bravo";
break;
// ... rest of cases for letters
case 'Z':
phonetic = "Zulu";
break;
default:
Scanner x = new Scanner(System.in);
number = kb.nextInt();
switch (number) {
case 1:
phonetic = "One";
break;
case 2:
phonetic = "Two";
break;
// ... rest of cases for numbers
case 8:
phonetic = "Eight";
break;
case 9:
phonetic = "Nine";
break;
}
}
System.out.println("You Entered " + letter + ". This letter indicates: " + phonetic);
System.out.println("You Entered" + number + ". This number indicates: " + phonetic);
}
}
Trying to use switch in strings by first coverting string into char and then apply switch but still didnt done it....here is my code..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
class HappyBirthday {
public static void main(String[] args) throws IOException {
String Month;
char[] Months = Month.toCharArray();
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter your month.");
Month = JOptionPane.showInputDialog("enter month");
String month1 = { "January", "feb"};
char[] month2 = month1.toCharArray();
// String s=month1.equals(Month);
// System.out.print(month2Array[0]);
switch (month2) {
case 0:
System.out.println("kool");
break;
case 1:
System.out.println("not kool");
break;
default:
}
}
}
/**
* if (month1[1].equals(Month)) System.out.println("kool"); else
* if(month1[0].equals(Month)) System.out.println("kooooooooooooool"); else
* System.out.println("Big kooooool");
**/
Have a look at this excellent article on the subject.
Currently, you can not switch on a String. The language specification is clear on what you can switch on:
JLS 14.11 The switch statement
SwitchStatement:
switch ( Expression ) SwitchBlock
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type, or a compile-time error occurs.
Depending on what you want to do, you can switch on each char of a String:
String s = "Coffee, tea, or me?";
int vowelCount = 0;
int punctuationCount = 0;
int otherCount = 0;
for (char letter : s.toUpperCase().toCharArray()) {
switch (letter) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowelCount++;
break;
case ',':
case '.':
case '?':
punctuationCount++;
break;
default:
otherCount++;
}
}
System.out.printf("%d vowels, %d punctuations, %d others",
vowelCount, punctuationCount, otherCount
); // prints "7 vowels, 3 punctuations, 9 others"
Note that switching on strings will be supported in Java 7.
You can't switch on a char[] type. Switch on char[0] and use case 'J': and so on (although - because some months start with the same letter, the algorithm would be sub-optimal)