Small Android application, which performs addition and other basic operations, and the OnClick() is as follows

@Override
public void onClick(View v) {
    switch (v.getId()) {
           case R.id.btnAdd: 
           isValidToProcess(1);
           break;
           ......
           /*Switch continues for all other operations like Subtraction,etc*/
    }
}

and my isValidToProcess() is as follows

private boolean isValidToProcess(int a) {
    String num1 = mEdit1.getText().toString();
    String num2 = mEdit2.getText().toString();

    if (num1.matches("") || num2.matches("")) 
    {
        ValueEmptyWarning();
    }
    else {
        float numa = Float.parseFloat(num1);
        float numb = Float.parseFloat(num2);
        switch (a) {

        case 1:
            addition(numa, numb);
            break;
              ......
           /*Switch continues for all other operations like Subtraction,etc*/
    }
}

My addition() function

public void addition(float numa, float numb) {
    answer = numa + numb;
    mEdit3.setText(String.valueOf(answer));
    Log.v(TAG, "Error at Subtraction");
}

This program is working fine for Float and Integer numbers, But the problem is, for both Integer and Float values the answer will be in fractions, For example Number1=2 and Number2=3 and the answer=5.0

Objective: If User inputs Integer, The decimal point should not be there.

Is this possible to get the type of Value which user has entered on EditText?

有帮助吗?

解决方案 2

For more control, try the basic OOP concept of overloading methods like

public float addition(float numa, float numb) {
    // will return float
    return numa + numb;
}

public int addition(int numa, float numb) {
    // explicitly cast to int
    return numa + (int) numb;
}

public int addition(float numa, int numb) {
    // explicitly cast to int
    return (int) numa + numb;
}

public int addition(int numa, int numb) {
    // will return int
    return numa + numb;
}

To examin your in put, try something like this...

public void examineInput(String input1, String input2) {

    // For both are float
    if (input1.indexOf(".") != -1 && input2.indexOf(".") != -1) {
        float numa = Float.parseFloat(input1);
        float numb = Float.parseFloat(input2);
        float ans = addition(numa, numb);
        Log.i(TAG, String.format("%f + %f = %f", numa, numb, ans));
    }

    // for first to be int and second to be float
    else if (input1.indexOf(".") == -1 && input2.indexOf(".") != -1) {
        int numa = Integer.parseInt(input1);
        float numb = Float.parseFloat(input2);
        int ans = addition(numa, numb);
        Log.i(TAG, String.format("%d + %f = %d", numa, numb, ans));
    }

    // for first to be float and second to be int
    else if (input1.indexOf(".") != -1 && input2.indexOf(".") == -1) {
        float numa = Float.parseFloat(input1);
        int numb = Integer.parseInt(input2);
        int ans = addition(numa, numb);
        Log.i(TAG, String.format("%f + %d = %d", numa, numb, ans));
    }

    // for both to be int
    else if (input1.indexOf(".") == -1 && input2.indexOf(".") == -1) {
        int numa = Integer.parseInt(input1);
        int numb = Integer.parseInt(input2);
        int ans = addition(numa, numb);
        Log.i(TAG, String.format("%d + %d = %d", numa, numb, ans));
    }
}

And the is the input to test this code, with output

examineInput("5.2", "6.2");    // 5.200000 + 6.200000 = 11.400000
examineInput("5", "3.6");      // 5 + 3.600000 = 8
examineInput("1.6", "5");      // 1.600000 + 5 = 6
examineInput("5", "5");        // 5 + 5 = 10

Note: you need to verify that examineInput always get valid numbers, not strings of non numaric characters...

Hope this helps to improve OOP concepts as well..:)

其他提示

first check for Integer.parseInt(numer) and catch for NumberFormatException . if it will parse it correctly then it is an integer else you can go for float.

I don't think there is an usable api in EditText for developer to get the type of value.You can find another way in JAVA,apache may provider some widget to handle this.

You can use String formatters in this case.

Formatting Numeric Print Output

For your case, you have to use a pattern like this.

DecimalFormat df = new DecimalFormat("0.##");
String finalAnswer = df.format(answer);

You can use the following code in your addition function if your answer is float

answer = numa + numb;
String answerString = String.valueOf(answer);
String decimalString = answerString.substring(answerString.indexOf(".") + 1);

if (Integer.parseInt(decimalString) == 0)
    answerString = answerString.substring(0, answerString.indexOf("."));

This will return a float value if your answer has other than 0 after the decimal or else it will return int in String

The user always enters String there is no other type entered using editText however, you need to check if the editText has (.) therefore it is float and then parse it as float otherwise, it is Integer and then parse it as Integer.

The code could look like the following.

if (num1.indexOf(".") != 0) {
    float numa = Float.parseFloat(num1);
    float numb = Float.parseFloat(num2);
}
else
{
    int numa = Integer.parseInt(num1);
    int numb = Integer.parseInt(num2);
}

by doing so the output for integers wont be in fraction style

P.S: You have to make sure that the keyboard only enters number. so your app doesn't crash while parsing.

hope this helps

The type of Value which user has entered on EditText will be always String only. but you can restrict user to enter any perticular type value by android:inputType="" property inside EditText.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top