John Oggy
John Oggy

Reputation: 319

How get variable type?

In my application I have two textfield and one submit. This app calculate integer value in this textfiels. But if I put there text - have error. How I can check this text field for int and how show message if thare no integers. My try:

EditText num1text=(EditText)findViewById(R.id.num1text);
EditText num2text=(EditText)findViewById(R.id.num2text);        
Float num1=Float.parseFloat(num1text.getText().toString());
Float num2=Float.parseFloat(num2text.getText().toString());

if (num1 instanceof Float && num2 instanceof Float)
{
Float answ = (num1 * num2) / 100;
TextView answer=(TextView)findViewById(R.id.ans);
answer.setText(answ);
}else
answer.setText("Message");

But my app still don't show my message in else case.

Upvotes: 0

Views: 85

Answers (4)

Raghunandan
Raghunandan

Reputation: 133560

Float.parseFloat(num2text.getText().toString()); if not float throws a number format exception. Catch the exception. Clear the fields. Display a toast message that the input is invalid.

public class MainActivity extends Activity {
TextView answer;
EditText num1text,num2text;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    num1text=(EditText)findViewById(R.id.editText1);
    num2text=(EditText)findViewById(R.id.editText2);   
    Button b= (Button) findViewById(R.id.button1);
    answer = (TextView) findViewById(R.id.textView2);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try
            {
            Float num2=Float.parseFloat(num2text.getText().toString());
            Float num1=Float.parseFloat(num1text.getText().toString());
                            //if the numbers are not float throws a numberformat exception
            if (num1 instanceof Float && num2 instanceof Float)
            {
            Float answ = (num1 * num2) / 100;
            answer.setText(Float.toString(answ));
            }else
            answer.setText("Message");
            }
            catch(NumberFormatException e)
            {
                               //catch the exception clear the fields and display toast
                num1text.setText("");
                num2text.setText("");
                Toast.makeText(MainActivity.this, "Invalid Input Type!Check the input", 1000).show();
                e.printStackTrace();
            }

        }

    });

}
}

Upvotes: 0

user2742371
user2742371

Reputation:

You have to use a regular expression:

if (YourVariable.matches("^[0-9,;]+$")) {
    // Contains only numbers
} else {
    // Contains NOT only numbers
}

Upvotes: 1

Barend
Barend

Reputation: 17444

As some others commented, you can use a regular expression to check the contents of the input field before you start converting them. This a good recommendation, you should scrub any user inputs before use.

If you don't, or can't: the Float.parseFloat() operation throws NumberFormatException if the input cannot be converted. You need to use a try...catch construct to avoid this.

try {
    Float num1=Float.parseFloat(num1text.getText().toString());
    Float num2=Float.parseFloat(num2text.getText().toString());
    Float answ = (num1 * num2) / 100;
    TextView answer=(TextView)findViewById(R.id.ans);
    answer.setText(answ);
} catch (NumberFormatException e) {
    TextView answer=(TextView)findViewById(R.id.ans);
    answer.setText("Message");
}

Furthermore, you can use the android:inputType attribute in your layout XML to restrict the input to numbers only (and get a numeric keyboard).

Upvotes: 4

Pratik
Pratik

Reputation: 30855

If the input was fixed that you have to need always integer/real (Number) value then you can set the inputtype in your xml file for EditText as attribute to number.

This will softkeyboard show only number keys on screen so you can getting always the number and no need to check for type whether user enter Character or Number value

For e.g. like this

<EditText android:inputType="number"
.../>

Upvotes: 0

Related Questions