Reputation: 85
The switch case always runs the default code. I read something about a "new line" issue, but i think that is not the case here. Because i copy the first char from the "stringclient" string into a char variable. But when i run this with my ATMServer class in eclpise, it's working just fine. Only when i execute them from cmd, this problem appears. So anyone knows what's going on? Please help. Thanks.
import java.io.*;
import java.net.*;
public class ATMClient {
private static final int PORT = 20000;
private static final char DRAW_STR = 'Α';
private static final char DEPOSIT_STR = 'Κ';
private static final char BALANCE_STR = 'Υ';
private static final char EXIT_STR = 'Ε';
private static boolean hasEnded = false;
public static void main(String args[]) throws IOException {
Socket dataSocket = new Socket("localhost",PORT);
InputStream is = dataSocket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
OutputStream os = dataSocket.getOutputStream();
PrintWriter out = new PrintWriter(os, true);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String stringclient;
while(!hasEnded){
printMenu();
stringclient = input.readLine();
char optionCode = stringclient.charAt(0);
String tempData;
int amount;
switch(optionCode){
case EXIT_STR:
out.println(String.valueOf(EXIT_STR));
hasEnded = true;
continue;
case DRAW_STR:
tempData = stringclient.substring(1);
try{
amount = Integer.parseInt(tempData);
}catch(NumberFormatException e){
System.out.println("Το ποσό πρέπει να είναι αριθμός. Δοκιμάστε ξανά.");
System.out.println();
continue;
}
if(amount > 420){
System.out.println("Μπορείτε να κάνετε ανάληψη έως 420 ευρώ.");
System.out.println();
continue;
}
out.println(String.valueOf(DRAW_STR) + amount);
break;
case DEPOSIT_STR:
tempData = stringclient.substring(1);
try{
amount = Integer.parseInt(tempData);
}catch(NumberFormatException e){
System.out.println("Το ποσό πρέπει να είναι αριθμός. Δοκιμάστε ξανά.");
System.out.println();
continue;
}
out.println(String.valueOf(DEPOSIT_STR) + amount);
break;
case BALANCE_STR:
out.println(String.valueOf(BALANCE_STR));
break;
default:
System.out.println("Λάθος επιλογή. Δοκιμάστε ξανά.");
System.out.println();
continue;
}
String reply = in.readLine();
System.out.println(reply);
}
out.close();
os.close();
in.close();
is.close();
input.close();
dataSocket.close();
}
}
Upvotes: 2
Views: 2648
Reputation: 11
I think it has something to do with the character encoding of the source file. Try compiling with the 'javac -encoding'. Also, I think when you run from Eclipse, it takes care of the character encoding when you run the application and input your value, where as with cmd, when you input your value it uses your default system encoding and that's why there is this inconsistency; only a guess though.
Upvotes: 1
Reputation: 85
I found a 'solution'. I used System.out.println(); to print the numerical codes of the four greek constants i use in the program. Then i assigned those numbers to the char constants instead of the characters and it worked.
Upvotes: 0
Reputation: 30819
It has something to do with final char
constants. Just tried debugging the code and it it returned ASCII value 913 for DRAW_STR
constant, which is character \u0391
in Java (i.e. Greek Alpha).
So, let's say, when user inputs 'A(150)', A has 65 ASCII
value whereas DRAW_STR
has 913 ASCII
value and hence, they do not match and control goes to default
block. The reason it works in eclipse can be the character set used by eclipse.
I fixed it by removing that character and re-typing again. It seems it may have been because of copy paste. However, if you want to remove that possibility then you can use unicode
representations of chatacters (e.g. private static final char DRAW_STR = '\u0041';
). Here is the unicode character table.
Upvotes: 0