Bharthish Athreya Bharthish Athreya - 1 year ago 49
Java Question

Why 'Res' Variable holding some Random numbers instead of retrieving the number from edit text field?

Below is the code:

public class MainActivity extends AppCompatActivity implements
View.OnClickListener {

private TextView TvTtlCrd;

int Res = TvTtlCrd.getText().length();
switch (view.getId()) {
try {
Float Calculation = (
(Float.parseFloat(numM1) * Float.parseFloat(numP1))
Float Result=Calculation/Res;
} catch (Exception e) {
TvRes.setText("Make sure to Enter all the fields");

When I debug the code I found that all variables retrieve the same number as in text field but the TvTtlCrd text field value is not retrieved. What is the reason?
I have posted only part of the code which is relevant.
also How to Store variables in List and calculate using list functions?

Answer Source

It appears that you are grabbing the length of the TvTtlCrd Object instead of the Text.


int Res = TvTtlCrd.getText().length();


int Res = Integer.parseInt(TvTtlCrd.getText().toString());

Or: and parse however you see fit to get the int value.

String Res = TvTtlCrd.getText().toString();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download