0

I am trying to build a Bengali calculator. Here is the layout of English calculator :

Layout of English calculator

By editing layout I can easily replace English digits with Bengali digits. But when it comes to the calculation i am unable to do it. Like i want it to calculate in Bengali too. e.g it will perform in Bengali like this (২+২=৪) instead of (2+2=4). I have tried the replacing method but it didn't work.

Bengali digits(০ ১ ২ ৩ ৪ ৫ ৬ ৭ ৮ ৯)
English digits(1 2 3 4 5 6 7 8 9)

Thank you for your time.

public class MainActivity extends AppCompatActivity {
    private TextView screen;
    private String str2, result, str, sign;
    private double a, b;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        screen = (TextView) findViewById(R.id.textview);
        str = "";
    }

    public void onclick(View v) {
        Button button = (Button) v;
        str += button.getText().toString();
        screen.setText(str);
        a = Double.parseDouble(str);
    }
    public void onclicksign(View v) {
        Button button = (Button) v;
        sign = button.getText().toString();
        screen.setText(sign);
        str = "";
    }
    public void calculate(View v) {
        Button button = (Button) v;

        str2 = screen.getText().toString();
        b = Double.parseDouble(str2);
        if (sign.equals("+")) {
            result = a + b + "";
        } else if (sign.equals("-")) {
            result = a - b + "";
        } else if (sign.equals("*")) {
            result = a * b + "";
        } else if (sign.equals("/")) {
            result = a / b + "";
        } else {
            screen.setText("?????? ???");
        }
        {
            screen.setText(result);

        }
    }
}
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • They are not English Digits. They are Numbers. You can do calculations only in numbers because computer doesn't understand Bengali digits. – ZP Baloch Apr 28 '17 at 13:38
  • okay they are numbers. But it is also possible to create a calculator other language. I just don't know how to do it. – Mehadi Hasan Apr 28 '17 at 13:41

2 Answers2

0

Your code tries to extract numerical values from the text on buttons.

You should write custom Double parser which parses Bengali number text to numerical value.

Also you should write a method which converts numerical double value to Bengali number text. You have to use this method while setting screen text.

public class MainActivity extends AppCompatActivity {
    private TextView screen;
    private String str2, result, str, sign;
    private double a, b;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        screen = (TextView) findViewById(R.id.textview);
        str = "";
    }

    public void onclick(View v) {
        Button button = (Button) v;
        str += button.getText().toString();
        screen.setText(str);
        a = BengaliUtils.toDouble(str);
    }
    public void onclicksign(View v) {
        Button button = (Button) v;
        sign = button.getText().toString();
        screen.setText(sign);
        str = "";
    }
    public void calculate(View v) {
        Button button = (Button) v;

        str2 = screen.getText().toString();
        b = BengaliUtils.toDouble(str2);
        if (sign.equals("+")) {
            result = a + b + "";
        } else if (sign.equals("-")) {
            result = a - b + "";
        } else if (sign.equals("*")) {
            result = a * b + "";
        } else if (sign.equals("/")) {
            result = a / b + "";
        } else {
            screen.setText("?????? ???");
        }
        {
            screen.setText(BengaliUtils.toString(result));

        }
    }
}

class BengaliUtils {
    static String toString(double value) {
        //TODO implement logic
        // You can convert value to regular number text, and then replace each char with the Bengali version. The performance could be improved with better logic.
        return text;
    }

    static double toDouble(String text) {
        //TODO implement logic
        //You can do that, first replace each Bengali char with normal number char. The use Double.parse on new text. The performance could be improved with better logic.
        return value;
    }
}
fthdgn
  • 1,339
  • 1
  • 13
  • 18
0
            @Override
            public void onClick(View view) {
                String num  = editText.getText().toString();
                //split the num

                char[] charArray = num.toCharArray();
                StringBuilder stringBuilder = new StringBuilder(charArray.length);

                // loop and convert using  switch case

                for (int i=0; i<charArray.length; i++ ){

                    char character = charArray[i];


                    switch (character){
                        case '.':
                            stringBuilder.append(".");
                            break;
                        case '0':
                            stringBuilder.append("০");
                            break;
                        case '1':
                            stringBuilder.append("১");
                            break;
                    }
                }
                //Final result..
                textView.setText(stringBuilder);

            }

Try above code...

Here is the output

enter image description here


NOTE

  • I am taking English numbers from EditText on Button click. You will have to change that in your code.
  • Currently, switch can handle 0,1,.(decimal) only. You can easily add cases for other numbers too.
  • Please Check this answer too. Convert String to another locale in java
Community
  • 1
  • 1
ray an
  • 1,132
  • 3
  • 17
  • 42
  • Switch statement does not go with TextView :( – Mehadi Hasan May 03 '17 at 12:55
  • You can use setText() in switch. If it is not working for you let me know. – ray an May 03 '17 at 13:35
  • Again switch statement does not support "double". calculator result must be "double". I am re-arranging the layout. I think you are from Bangladesh.If you give me your mail id i can send you the layout and the code. So the problem can be more specific. Thank you. – Mehadi Hasan May 03 '17 at 14:43
  • the above code will only work if the result is <=9. If the result is >9 then you will have to write some extra code for splitting the result into individual digits and then passing each digit to switch expression and appending the corresponding Bengali digits to the textView – ray an May 03 '17 at 15:29
  • http://stackoverflow.com/questions/5316131/convert-string-to-another-locale-in-java --- Check this question. – ray an May 03 '17 at 15:30