0

So first of all sorry if this has already been asked and answered before, I couldn't find anything relating to my issue.

So I'm working on a project for college and I need to get int values from EditText widgets. I was told to use parseInt to do this however when running my program, that line of code causes the application to crash. I don't know what I'm doing wrong, I'm still very new to android development, thanks for the help :)

public void Calculate (View view)
{
    int MilesTravelled;
    int FuelUsed;
    int MPG;

    /* the two lines below are what cause the application to crash */

    MilesTravelled = Integer.parseInt(txtMilesTravelled.getText().toString());
    FuelUsed = Integer.parseInt(txtFuelUsed.getText().toString());

    FuelUsed = (int) (FuelUsed / 4.55);
    MPG = MilesTravelled / FuelUsed; 
    lblMPG.setText(FuelUsed); 
}
mmBs
  • 8,421
  • 6
  • 38
  • 46
user3254643
  • 11
  • 1
  • 3

3 Answers3

1

Do you have this in the onCreate() function?

 EditText txtMilesTravelled = (EditText) findViewById(R.id.YourEditText);

But I think you mixed Integer and int. They are not the same:

See this link!

Community
  • 1
  • 1
UhrArt
  • 504
  • 5
  • 8
  • Ah and you should make the int to a string before you set the text here: lblMPG.setText(FuelUsed.toString()); – UhrArt May 03 '14 at 20:28
1

First of all, don't capitalize the first letter of an variables or method names. Following the Java coding conventions, only do that for classes.

What is probably causing your app to crash is you trying to set the text of a label to an integer. The setText method for a TextView needs to take in a string.

So change:

lblMPG.setText(FuelUsed);

to:

lblMPG.setText(String.valueOf(FuelUsed)); 

Otherwise it might be that it's trying to parse a non-numerical string to an integer. For exmaple, if the EditText is blank, it will cause your app to crash. To prevent that, try this:

int MilesTravelled = 0, FuelUsed = 0;
try {

    MilesTravelled = Integer.parseInt(txtMilesTravelled.getText().toString());
    FuelUsed = Integer.parseInt(txtFuelUsed.getText().toString());

} catch (NumberFormatException nfe) {

    Toast.makeText(getApplicationContext(), "Error NFE!", 0).show();
    nfe.printStackTrace();
}

This way, it will catch a NumberFormatException error (parsing a string to an integer that can't be represented as an integer, such as "hello"). If it catches the error, it will toast that an error has occurred and your integer variables will remain 0.


Or you could just test if the strings contain only digits using the following regex:

int MilesTravelled = 0, FuelUsed = 0;

if (txtMilesTravelled.getText().toString().matches("[0-9]+")) {
    MilesTravelled = Integer.parseInt(txtMilesTravelled.getText().toString());
} else {
    // contains characters that are not digits
}
if (txtFuelUsed.getText().toString().matches("[0-9]+")) {
    FuelUsed = Integer.parseInt(txtFuelUsed.getText().toString());
} else {
    // contains characters that are not digits
}

If that's not the problem, then make sure you define your variables properly.

txtMilesTravelled and txtFuelUsed should be EditText:

EditText txtMilesTravelled = (EditText)findViewById(R.id.txtMilesTravelled);
EditText txtFuelUsed = (EditText)findViewById(R.id.txtFuelUsed);

And make sure that your R.id.editText actually exists on your layout and that the IDs are the correct ones.


Last thing, make sure FuelUsed is not 0 before calculating MPG because then you are dividing by 0:

int MPG = 0;

if (FuelUsed != 0) {
    MPG = MilesTravelled / FuelUsed; 
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
1

I am assuming that you're entering perfect integers in the EditTexts. It might be a good idea to use the trim function txtMilesTravelled.getText().toString().trim() before using parseInt.

However, I think the major problem is here : lblMPG.setText(FuelUsed); FuelUsed is an integral value, when you pass an integer to setText(), it looks for a string resource with that integral value. So you should be passing a String to the setText() method.

Use : lblMPG.setText(Integer.toString(FuelUsed));

Software Engineer
  • 3,906
  • 1
  • 26
  • 35
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34