-1

I want to toast an integer in an IF condition about "isChecked" action. Now, when I execute I'm getting wrong number.

  • For the 'if', the value of 5 (inherits from etSNum) toast 0.95
  • For the 'else if', the value of 5 (inherits from etSNum) toast -5

code:

Button btnWaitress;
EditText etSalaryWaitress;
EditText etSalaryBartender;
//------------
RadioButton rbPercentage;
RadioButton rbShekel;
int HafrashaP;
int HafrashaS;
int etSWNum;
String strHafPer;
String strHafShek;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    btnWaitress =(Button)findViewById(R.id.btnWaitress);
    etSalaryWaitress = (EditText) findViewById(R.id.etSalaryWaitress);
    etSWNum = Integer.parseInt(etSalaryWaitress.getText().toString());
    etSalaryBartender = (EditText) findViewById(R.id.etSalaryBartender);
    //-----------
    rbPercentage = (RadioButton)findViewById(R.id.rbPercentage);
    rbShekel = (RadioButton)findViewById(R.id.rbShekel);

}
public void onClickWait (View v) {

    if (rbPercentage.isChecked()) {
        HafrashaP = 1 - (etSWNum / 100);
        strHafPer= Integer.toString(HafrashaP);
        Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();
    }   else if (rbShekel.isChecked()) {
            HafrashaS = - etSWNum;
            strHafShek= Integer.toString(HafrashaS);
        Toast.makeText(settings.this, strHafShek, Toast.LENGTH_SHORT).show();
        }  else {
            Toast.makeText(settings.this, "XXXXXXX", Toast.LENGTH_SHORT).show();
                }
}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ofek Biton
  • 15
  • 4
  • what do you want? – Ajay Mistry Aug 29 '17 at 13:36
  • You should tell us what value it is giving. – bendl Aug 29 '17 at 13:59
  • It's an app about waitress' salary. I want to toast once the answer as message, and then to use it again to more calculates. For the **value of 5** that came from etSalaryWaitress : if **rbPercentage* is checked, toast HafrashP = 0.95 (aka -5%). if *rbShekel* is checked, toast HafrashaS = - 5 (aka - 5 dollars) – Ofek Biton Aug 29 '17 at 14:04
  • it still returns wrong answer. In addition to after 1 click it still adds the numbers but with errors like: 1.00.00.00 . – Ofek Biton Aug 30 '17 at 13:49

2 Answers2

1

You are performing an integer division operation. 1 / 100 = 0 always when considering integers. You need to use a double, so you should be doing something like:

double percent = 1 - (etSWNum / 100.0);
AkiAki007
  • 429
  • 3
  • 16
  • Ok, I will try it. What about the 'if else' path? for value of 5, it gets 0 @aki – Ofek Biton Aug 29 '17 at 13:50
  • I tried it. Changed integer to double, but how I make it string? I want to toast it once time as a string, and then use it again to do more math actions as double. About the 'else if' path , whice solution would you recommend? – Ofek Biton Aug 29 '17 at 14:00
  • String.format(...) is what you should use to format numbers into a String. Please look up the Android or Java documentation for this and how to accomplish this. As for the else-if section, I don't know what you intend to display here, but I don't see a problem. – AkiAki007 Aug 30 '17 at 17:54
0

If you want to make an Integer as a String instead of using toString() method, you can also do this:

int i=5;
String s="";
s+=5; 

This is actually concatenating '5' with the null String 's' resulting in the value of 's' as :

s="5";

After this, you can make a Toast with the String S.

String strHafPer="";
strHafPer+=HafrashaP;
Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();
Pronoy999
  • 645
  • 6
  • 25