Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
public int alphCheck(char check){
switch(check){
case 'a':
return 1;
break;
case 'b':
return 2;
break;
case 'c':
return 3;
break;
case 'd':
return 4;
break;
case 'e':
return 5;
break;
case 'f':
return 6;
break;
case 'g':
return 7;
break;
case 'h':
return 8;
break;
case 'i':
return 9;
break;
case 'j':
return 10;
break;
case 'k':
return 11;
break;
case 'l':
return 12;
break;
case 'm':
return 13;
break;
case 'n':
return 14;
break;
case 'o':
return 15;
break;
case 'p':
return 16;
break;
case 'q':
return 17;
break;
case 'r':
return 18;
break;
case 's':
return 19;
break;
case 't':
return 20;
break;
case 'u':
return 21;
break;
case 'v':
return 22;
break;
case 'w':
return 23;
break;
case 'x':
return 24;
break;
case 'y':
return 25;
break;
case 'z':
return 26;
break;
}
}
PS.This was done in another class
I want to be able to use this method in the main class, to input a letter, and return a number/index for that letter.
But I kept getting: this method must return a result of type int.
Very Confused. Please help. Thx.
Here's a question to consider: What happens if the inputted letter isn't one of the cases you described?
While you may know that you're only feeding in letters, the compiler doesn't know that, and because it can't figure out what to return if one of the cases you defined isn't hit, emits an error as a result. You'll need to put in a default case, so the compiler knows that the method is guaranteed to return something:
switch(check) {
case 'a':
...
default:
// return something or maybe print/throw an error
}
A better solution for this may be to use the fact that chars are just numbers in a different form. For example, 'a' is equivalent to the integer 97 (check out the table here for a table of characters and their ASCII numerical equivalents). So you can do a math trick to get equivalent results:
public int alphCheck(char check) {
return check - 'a' + 1;
}
you have to provide return type like return 0; at the end of your switch statement.or in default: case
switch(check) {
..
default:
return 0;
}
Related
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.
So I am writing code that will take a character and return its alpha-numeric keypad equivalent as a character.
The problem is, I am getting a question mark in a box as a return. I have checked that the input is correct. For example, with a char 'h' input, i should get a char '4' return. Hoping someone is able to spot my error.
Code is below:
public char getDigit(char letter) throws Exception{
switch (letter) {
case 'a': case 'b': case 'c': case '2':
return 2;
case 'd': case 'e': case 'f': case '3':
return 3;
case 'g': case 'h': case 'i': case '4':
return 4;
case 'j': case 'k': case 'l': case '5':
return 5;
case 'm': case 'n': case 'o': case '6':
return 6;
case 'p': case 'q': case 'r': case 's': case '7':
return 7;
case 't': case 'u': case 'v': case '8':
return 8;
case 'w': case 'x': case 'y': case 'z': case '9':
return 9;
default:
throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");
}
}
The return type of your method is char.
Now take your switch statement. You return char values in the range from 2 to 9. Now look at an ASCII table.
Surprise: these chars are all "unprintable" control characters. Thus your console gives you "?" when you print them!
If you want '4', your code has to return '4', not 4! Or 52, because that entry in the ASCII table represents '4'.
You are not getting proper output because you are using char as a return type of getDigit(..) function. You should use int as a return type instead of char as in switch case you are comparing with characters and returning digit values So replace your code with the following code, this will work:
public int getDigit(char letter) throws Exception{
switch (letter) {
case 'a': case 'b': case 'c': case '2':
return 2;
case 'd': case 'e': case 'f': case '3':
return 3;
case 'g': case 'h': case 'i': case '4':
return 4;
case 'j': case 'k': case 'l': case '5':
return 5;
case 'm': case 'n': case 'o': case '6':
return 6;
case 'p': case 'q': case 'r': case 's': case '7':
return 7;
case 't': case 'u': case 'v': case '8':
return 8;
case 'w': case 'x': case 'y': case 'z': case '9':
return 9;
default:
throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");
}
}
I want to convert bytecode type to java using an api instead of manually coding for it, Is there any api for that? Since the requirement goes high I have to keep adding the convertion type manually which is bit tedious.
[Ljava/lang/StackTraceElement;
J[Ljava/lang/StackTraceElement;
Ljava/util/Map;
Ljava/util/LinkedList;
Ljava/lang/String;
Ljava/net/URL;
Z
Ljava/lang/String;
[Ljava/net/URL;
Previously I used code it manually as below.
private static Type getType(final char[] buf, final int off) {
int len;
switch (buf[off]) {
case 'V':
return VOID_TYPE;
case 'Z':
return BOOLEAN_TYPE;
case 'C':
return CHAR_TYPE;
case 'B':
return BYTE_TYPE;
case 'S':
return SHORT_TYPE;
case 'I':
return INT_TYPE;
case 'F':
return FLOAT_TYPE;
case 'J':
return LONG_TYPE;
case 'D':
return DOUBLE_TYPE;
}
}
And when I used asm library for this,
Type.getArgumentTypes(desc);
it showed me an error as below.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 44
at org.objectweb.asm.Type.getArgumentTypes(Unknown Source)
I would add a method getTypeLength:
private static int getTypeLength(final char[] buf, final int off) {
switch (buf[off]) {
case 'V':
case 'Z':
case 'C':
case 'B':
case 'S':
case 'I':
case 'F':
case 'J':
case 'D':
return 1;
case 'L':
{
int i = offs+1;
while (buf[i] != ';') {
++i;
}
return i-offs+1;
}
case '[':
return getTypeLength(buf, off+1)+1;
}
}
And then:
private static Type getType(final char[] buf, final int off) {
int len;
switch (buf[off]) {
case 'V':
return VOID_TYPE;
case 'Z':
return BOOLEAN_TYPE;
case 'C':
return CHAR_TYPE;
case 'B':
return BYTE_TYPE;
case 'S':
return SHORT_TYPE;
case 'I':
return INT_TYPE;
case 'F':
return FLOAT_TYPE;
case 'J':
return LONG_TYPE;
case 'D':
return DOUBLE_TYPE;
case 'L':
{
int len = getTypeLength(buf, offs);
String name = new String(buf, offs+1, len-2).replace('/', '.');
return Class.forName(name);
}
case '[':
int len = getTypeLength(buf, offs);
return Class.forName(new String(buf, offs, len));
}
}
If you are using asm it would be like this,
Type.getType(desc).getClassName();
Type class from asm library and its static method getType which accepts a string will convert bytecode type to java type.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to manipulate my mother language mathematical number in Java.
Example:
int a = 1;
int b = 2;
int c = a + b;
System.out.println(c);
I want to add and show these number in my language. As in the following:
int a = ၁;
int b = ၂;
int c = a + b;
System.out.println(c);
supposed that 'a' through 'j' represent 0 to 9 in your mother language and numbers are written left to right.
it's better if you convert the input and output to normal integers and work with them. because this way you have access to all Mathematical methods that java provides and more important java's libraries are more robust than your own methods.
this two method will convert String object(your wanted characters to int)
to int and Vice versa.
public static String convertToString (int value){
StringBuilder result = new StringBuilder();
for (int i=1;value/i>0;i *= 10)
switch( value % (i*10) / i){
case 0:
result.append('a');
break;
case 1:
result.append('b');
break;
case 2:
result.append('c');
break;
case 3:
result.append('d');
break;
case 4:
result.append('e');
break;
case 5:
result.append('f');
break;
case 6:
result.append('g');
break;
case 7:
result.append('h');
break;
case 8:
result.append('i');
break;
case 9:
result.append('j');
break;
}
return result.reverse().toString();
}
public static int convertToInt(String string){
int result = 0;
int j =1;
for (int i=string.length()-1;i>=0;i--,j *= 10)
switch(string.charAt(i)){
case 'b':
result += 1*j;
break;
case 'c':
result += 2*j;
break;
case 'd':
result += 3*j;
break;
case 'e':
result += 4*j;
break;
case 'f':
result += 5*j;
break;
case 'g':
result += 6*j;
break;
case 'h':
result += 7*j;
break;
case 'i':
result += 8*j;
break;
case 'j':
result += 9*j;
break;
}
return result;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want java code using switch to same execute ? how i can do this by java
if(ave>=90.0)
return 'A';
else if(ave>=80.0)
return 'B';
else if(ave>=70.0)
return 'C';
else if(ave>=60.0)
return 'D';
else
return 'F';
The intuitive solution is: It's impossible.
A switch needs a discreet set of elements. A range of numbers is infinite and you can't do
switch(something) {
case 90.0:
case 90.000000000001:
....
There is a way you could do that though: Convert the range to some number:
private static int toRangeIndex(double d) {
if (d >= 90.0)
return 0;
else if (d >= 80.0)
return 1;
else if (d >= 70.0)
return 2;
else if (d >= 60.0)
return 3;
else
return 4;
}
public static double sumColoumn(double[][] m, int coloumnIndex) {
switch (toRangeIndex(ave)) {
case 0:
return 'A';
case 2:
return 'B';
case 3:
return 'C';
case 4:
return 'D';
default:
return 'F';
}
}
This is obviously not better in your case. But there are cases in which you could use such a technique.
Not possible directly, switch requires exact match.
What you can do is write function like:
int classify(double avg) {
// perform some if-else chain, or loop with test inside, or calculation:
return (int)(avg/10.0);
}
Then use the return value in switch:
switch (classify (avg)) {
case 10: // average of exact 100.0 gives 10, let's not F that...
case 9:
return 'A';
case 8:
return 'B';
//...
default:
return 'F';
}
But, in your specific case it is just moving the if... ladder into a different function, and probably not good idea. So don't do it :-).
Or rather, if you do it, do it because it makes code easier to understand and maintain (and here it in my opinion does not), not because you want to use switch statement.
int findIndex(double ave){
int index=(int)(ave/10.0);
if(index>=9)
return 9;
else
return index;
}
switch (findIndex(ave)) {
case 9:
return 'A';
case 8:
return 'B';
case 7:
return 'C';
case 6:
return 'D';
default:
return 'F';
}
i finded answer
switch(t1)
{
case 100: case 99: case 98: case 97:case 96:case 95:case 94:case 93:case 92:case 91:case 90:
cr='A';
break;
case 89: case 88: case 87: case 86:case 85:case 84:case 83:case 82:case 81:case 80:
cr='B';
break;
case 79: case 78: case 77: case 76:case 75:case 74:case 73:case 72:case 71:case 70:
cr='C';
break;
case 69: case 68: case 67: case 66:case 65:case 64:case 63:case 62:case 61:case 60:
cr='D';
break;
default:
cr = 'F';
break;
}