-2

I am working on an android app and need some help with a calculation code. A user is required to enter a value 1-5000000 into a text field (EditText), a button (calculate) is pressed, when this happens the entered number needs to be multiplied by 10% and the answer displayed on screen in a text field (EditText). Below is the code i am working on:

    public void operation(){
    if(optr.equals("*")){
        amt = Integer.parseInt(num.getText().toString());
        amt = num * 10%;
        num1.setText(Integer.toString(amt));
    }
Pooveshin
  • 203
  • 5
  • 13

5 Answers5

2

Try this:

int amt = Integer.parseInt(num.getText().toString());
int result = amt/10;

Btw, you are trying to multiply your EditText and not its value in your code.

longwalker
  • 1,614
  • 2
  • 13
  • 18
2

Use the below code:

public void operation(){
    if(optr.equals("*")){ // what is the optr?
        amt = Integer.parseInt(num.getText().toString());
        amt *= 10%; //You must use the value here, not the EditText
        num1.setText(String.valueOf(amt));
    }
}
Kelevandos
  • 7,024
  • 2
  • 29
  • 46
0

Try this; % is not a percent operator in programming context.

public void operation(){
if(optr.equals("*")){
    amt = Integer.parseInt(num.getText().toString());
    amt = num / 10;
    num1.setText(Integer.toString(amt));
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sameer Khan
  • 637
  • 6
  • 15
0

You need to add a text change listener like in the answer here: android edittext onchange listener

you can also use RXJava and RXbinding to create an observable that emits the value of the edittext whenever it is changed

EDIT: nevermind didnt notice you were running this from a method

Community
  • 1
  • 1
Will Evers
  • 934
  • 9
  • 17
0

In Java you can not multiply directly with 10% to get the 10% of any number.

Changes have been made in your code by commenting your line. considering float as input.

public void operation(){
    if(optr.equals("*")){
        //amt = Integer.parseInt(num.getText().toString());
        // amt = num * 10%;
        amt = Float.parseFloat(num.getText().toString());
        amt = num/10;
        num1.setText(amt+"");

        //num1.setText(Integer.toString(amt));
    }
}
ketankk
  • 2,578
  • 1
  • 29
  • 27