0

Running the below code and can't resolve StringUtils and myString... does something need imported or is there another way? this takes a string from another activity and makes it an integer to allow a calculation of old and new values.

   protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Setting button as a unique name
            Button buttonAdd = (Button) findViewById(R.id.add_button);
            Button buttonAbout = (Button) findViewById(R.id.about_button);
            Button buttonReset = (Button) findViewById(R.id.reset_button);


            String calorie = getIntent().getStringExtra("calorie");
            TextView textView1 = (TextView)findViewById(R.id.textView1);

            String strOldValue = textView1.getText().toString();

            Integer oldValue = StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;
            Integer newValue = Integer.parseInt(calorie);



            textView1.setText((oldValue + newValue));
ERN
  • 115
  • 1
  • 8

2 Answers2

2

The most of safe way is used to try {} catch {} block like code below

Integer oldValue;
try{
 oldValue= Integer.parseInt(myString);
}catch(NumberFormatException e){
  oldValue =0;
}

instead of :

 Integer oldValue = StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
0

To add to Konrad's answer:

You should include all your code...

if you are trying to Get a String from a previous Activity then parse it to an Integer, Why not just pass it as an int And do something like:

Declare class level variable

private int mPassedInValue;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

 // Get the Extras Bundle from the Intent
 Bundle previousIntentExtras  = getIntent().getExtras();

 // Check to make sure this is not null
 if(previousIntentExtras != null){
    mPassedInValue = previousIntentExtras.getInt("MyInt",0);

  /* ... Perform the function you want to do since the value you passed in is
   * Is already in the correct form...
   */ 

 }

} 
kandroidj
  • 13,784
  • 5
  • 64
  • 76