将字符串转换为整型以在Java Android中查找结果。

huangapple go评论74阅读模式
英文:

Converting string into int to find a result in java android

问题

如何将类似于 +-/* 这些字符,从 String 转换为 int 呢?
我尝试使用 int 找到一些结果,但是这些字符 +-/* 导致出现错误,
当我尝试从 String 转换为 int 时,

通常情况下,当你输入 int i = 12+12 时,它会显示结果 24
但是当我尝试将其从 String 转换为 int 时,我的应用程序会强制关闭,有什么建议吗?谢谢。

英文:

how to convert some character like this +,-,/,*, from String into an int,
i try to find some result using int, but this character +,-,/,* make it error,
when i am trying to convert from String into int,

normally when you type int i = 12+12 it will display the result of 24,
but when i am trying to convert it from String to int, my app force close, any advice?, thank you

答案1

得分: 1

使用ScriptEngine库是一种简单的方法 -

转到build.gradle(Module:app)。
添加这个依赖 - implementation 'io.apisense:rhino-android:1.0'

然后,要计算任意字符串的值,执行以下步骤 -

对于所有操作(+ - * \ %),使用相同的代码,只需更改字符串的值。

String s = "12+12";
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("rhino");
try {
    Object result = scriptEngine.eval(s);
    System.out.println("Result: " + result); // 结果(输出)为: 24
} catch (ScriptException e) {
    e.printStackTrace();
}

示例 -
当用户在EditText中输入12+12时,将其存储在一个String s = editText.getText().toString()中。

调用方法 - String result = calculateResult(s);

该方法是 -

private String calculateResult(String s) {
    ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("rhino");
    Object result = null;
    try {
        result = scriptEngine.eval(s);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
    return result.toString();   // 返回 24
}
英文:

Easy approach should be using ScriptEngine Library -

Go to build.gradle(Module:app).
Add this dependency - implementation 'io.apisense:rhino-android:1.0'

Then To calculate value of any string, do this -

Use same code for all operation(+ - * \ %), just change the string value.

    String s = "12+12";
    ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("rhino");
    try {
        Object result = scriptEngine.eval(s);
        System.out.println("Result: "+result); // Result(Output) is: 24
    } catch (ScriptException e) {
        e.printStackTrace();
    }

Example -
When user enters in EditText 12+12 take it into a String s = editText.getText().toString()

Call a method - String result = calculateResult(s);

The method is -

private String calculateResult(String s) {
        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("rhino");
        Object result = null;
        try {
            result = scriptEngine.eval(s);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return result.toString();   // returns 24
    }

答案2

得分: 1

代替Integer.parseInt(getTextView);,你将需要首先从从TextView获取的字符串中提取数字,然后将它们分别转换为整数,然后进行算术运算。

请按以下方式进行操作。

equal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String getTextView = textView.getText().toString();
        String[] numbers = getTextView.split("\\+");
        int value = Integer.parseInt(numbers[0]) + Integer.parseInt(numbers[1]);
        textView.setText(String.valueOf(value));  
    }
});

更新

String[] numbers = getTextView.split(""+"");

替换为

String[] numbers = getTextView.split(""\\\\+"");

以避免悬挂元字符错误。

英文:

Instead of Integer.parseInt(getTextView); , you will have to first extract the numbers from the string obtained from the TextView and then convert them to integer separately and then do the arithmetic operation.

Do it as below.

equal.setOnClickListener(new View.OnClickListener() {
    @Override             
    public void onClick(View v) {
        String getTextView = textView.getText().toString();
        String[] numbers = getTextView.split("+");
        int value = Integer.parseInt(numbers[0]) + Integer.parseInt(numbers[1]);
        textView.setText(value);  
    }
}

UPDATE
Replace

String[] numbers = getTextView.split("+");

With

String[] numbers = getTextView.split("\\+");

to prevent the dangling metacharacter error.

答案3

得分: 1

按照以下方式进行操作:

// 从textView上按'+'分割字符串。为了指定'+'前后的可选空格,使用\\s*
String[] nums = textView.getText().toString().split("\\s*\\+\\s*");

// 将每个数字解析为整数,相加,然后将结果设置到textView中
textView.setText(Integer.parseInt(nums[0]) + Integer.parseInt(nums[1]));
英文:

Do it as follows:

// Split the string from textView on '+'. In order to specify optional space before/after '+', use \\s*
String[] nums = textView.getText().toString().split("\\s*\\+\\s*");

// Parse each number into an integer, add them and then set the result into textView
textView.setText(Integer.parseInt(nums[0]) + Integer.parseInt(nums[1]));  

huangapple
  • 本文由 发表于 2020年9月6日 17:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63762553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定