ibnouf88
ibnouf88

Reputation: 3

int EditText & if/else Statement to get a string, what is wrong?

My code is about if the user entering the pH number (normal pH=7.35-7.45) it will give him the answer were is it acidic pH or alkaline pH or normal, then I want to save a string called phStatus as an answer to be called with other strings in the end in the resultTextView after the onclick method, here it is:

Button resultButton = (Button) findViewById ( R.id.resultButton );
        final TextView resultTextView = (TextView) findViewById ( R.id.resultTextView );

        resultButton.setOnClickListener ( new OnClickListener ( ) {
            @Override
            public void onClick ( View v )
                            {
            EditText phET = (EditText) findViewById ( R.id.phEditText );
            int phEntered = Integer.parseInt ( phET.getText ( ).toString ( ) );
            String phStatus = "";
            if ( phEntered < 7.35 )
                {phStatus = "Acidosis";}
            else if ( phEntered > 7.45 )
                {phStatus = "Alkalosis";}
            else 
                {phStatus = "Normal acid-base balance";}


                String resultMSG = "This is " + phStatus;

                                resultTextView.setText ( resultMSG );
                            }   

Upvotes: 0

Views: 2789

Answers (3)

ibnouf88
ibnouf88

Reputation: 3

I figured it out with the help of you guys, thanks alot, now this is the code:

public class ABG extends Activity { @Override protected void onCreate ( Bundle savedInstanceState ) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.abg );

        }
    String phStatus;

    @Override
    public String ph ( String phStatus )
        {
            EditText phET = (EditText) findViewById ( R.id.phEditText );
            try
                {
                    double phEntered = Double.parseDouble ( phET.getText ( ).toString ( ) );
                    if ( phEntered < 7.35 )
                        {
                            phStatus = "Acidosis";
                        }
                    else if ( phEntered > 7.45 )
                        {
                            phStatus = "Alkalosis";
                        }
                    else
                        {
                            phStatus = "Normal acid-base balance";
                        }

                }
            catch (Exception e)
                {
                    // exception handle here
                }
            return phStatus;
        }



    @Override
    public void result ( View v )
        {
            // TODO Auto-generated method stub

            String resultMSG = "This is " + ph ( phStatus );

            TextView resultTextView = (TextView) findViewById ( R.id.resultTextView );
            resultTextView.setText ( resultMSG );
        }    
}

Upvotes: 0

Shivam Kamat
Shivam Kamat

Reputation: 121

Comparing int to a double is valid - it will promote the int to a double before performing the comparison. You just need a try catch as mentioned by @deepak.. You can also try assigning a default string (maybe "0")value to your EditText.

Upvotes: 1

Deepak Goyal
Deepak Goyal

Reputation: 4917

try this...
Add try catch block to handle exceptions.

Button resultButton = (Button) findViewById ( R.id.resultButton );
    final TextView resultTextView = (TextView) findViewById ( R.id.resultTextView );

    resultButton.setOnClickListener ( new OnClickListener ( ) {
        @Override
        public void onClick ( View v ) {
          // TODO Auto-generated method stub
          EditText phET = (EditText) findViewById ( R.id.phEditText );
          try{
            double phEntered = Double.parseDouble ( phET.getText ( ).toString ( ) );
            String phStatus = "";
            if ( phEntered < 7.35 ) {
               phStatus = "Acidosis";
            }else if ( phEntered > 7.45 ) {
               phStatus = "Alkalosis";
            }else {
               phStatus = "Normal acid-base balance";
            }
             String resultMSG = "This is " + phStatus;
             resultTextView.setText ( resultMSG );
             Toast.makeText ( getApplicationContext ( ), "Not Long Enough :(", 1000 ).show ( );
          }catch(Exception e){
            // exception handle here
          }
       }    

Upvotes: 2

Related Questions