2

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();
    }
}
1
  • 2
    You should log what the character. Commented Mar 3, 2016 at 23:11

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

No change. It still works in eclpise, but in cmd it doesn't. I don't get it how it works in eclpise though.
Can you try this: BufferedReader input = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
Nope. Still nothing. But when i replaced the "optionCode" variable inside the switch expression with 'Α' it run the DRAW_STR case normally. It seems that when the switch expression is not a variable it works.
How are you running your app in "cmd"? are you using command line to execute your class?
Yes. It's a simple cmd program, some programming homework. It's like an echo client-server, "simulating" an ATM.
|
0

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.

7 Comments

It's not working. And besides the "stringclient" string is user input and i always type that with capitals.
What is the user input provided to this program?
This program simulates an ATM. The user input is 'Α(ammount)' to draw money, 'Κ(ammount)' to deposit, 'Υ' to show the balance and 'Ε' to exit. (Those Α,Κ,Υ,Ε are greek letters). For example, when i type 'Α150', it means draw 150 ammount of money. The default in switch is for anything else the user types and shows an error message to the user.
When i run this in eclipse it works fine. But from cmd yes, it doesn't work in any case. It always runs default and prints my error message.
I didn't copy those greek characters. I typed them from the keyboard (my keyboard has greek layout too). And at the user input i type Α150 with greek alpha, so how the ascii code is 65 when i type with greek layout?
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.