【问题标题】:How to get the length of an integer in Java (Android)如何在Java(Android)中获取整数的长度
【发布时间】:2014-06-12 19:30:05
【问题描述】:

所以如果我的值是 1234,那么输出应该是 4。

我已经编写了以下代码,但不知道我做错了什么。 这是一个应用程序,它添加了两个数字。所以我有两个 EditTextfields。但如果 EditTextfield 留空,应用程序就会崩溃。 所以我想将值 0 分配给 EditTextfield,该字段已留空,因为它(应该)具有 0 的字符长度。

这应该会得到正确的结果:

例如,如果 EditTextfield "firstnumET" 的值为 5 并且 "secondnumET" 为空:

5 + 0 = 5

问题可能是


private int firstnum;
private int secondnum;
private int total;


EditText firstnumET;
EditText secondnumET;
TextView ansTV;
Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cal2_numbers);


    firstnumET = (EditText) findViewById(R.id.edittxt1);
    secondnumET = (EditText) findViewById(R.id.edittxt2);
    ansTV = (TextView) findViewById(R.id.ans);
    button = (Button) findViewById(R.id.btn);


    button.setOnClickListener(new ClickButton());



}

    private class ClickButton implements Button.OnClickListener {

    @Override
    public void onClick(View v) {


        firstnum = Integer.parseInt(firstnumET.getText().toString());
        secondnum = Integer.parseInt(secondnumET.getText().toString());

        int cache1 = Integer.toString(firstnum).length();
        int cache2 = Integer.toString(secondnum).length();
        if (cache1 == 0) {
            ansTV.setText(Integer.toString(secondnum));
        }
        if (cache2 == 0) {
            ansTV.setText(Integer.toString(firstnum));
        }



        total = firstnum + secondnum;

        ansTV.setText(Integer.toString(total));
    }
}

【问题讨论】:

    标签: android onclicklistener variable-length


    【解决方案1】:

    评论太长了,你为什么不检查你的字符串,如果它们不为空,则根据它们的长度设置它们。

    String firstString = firstnumET.getText().toString();
    firstNum = (firstString.trim().equals(""))? 0: firstString.length();
    
    String secondString = secondnumET.getText().toString();
    secondNum = (secondString.trim().equals(""))? 0: secondString.length();
    
    ansTV.setText(Integer.toString(firstNum + secondNum));
    

    你显然可以让它比这更复杂,检查以确保字符串只包含数值,以确保你没有得到非数字字符串的长度。

    String.trim 删除空格,这有助于避免编辑文本被空格填充。

    【讨论】:

    • 您同时使用 String.length() 和 Integer.parseInt() 是否有原因?我很确定 length() 只能返回一个整数。
    • @camdroid 很快就写出了解决方案并犯了一个愚蠢的错误:P
    • EditText.getText().toString() 如果没有输入任何内容,则返回 null,而不是 ""。您的 ?: 语句将始终评估为 false。
    • getText() 实际上从不返回 null。它返回超类 CharSequence 并返回在构造函数中初始化为 "" 的文本。
    • 看起来我前段时间遇到的一个错误并不是我认为的那样。感谢您的信息。
    【解决方案2】:

    你的问题在这里:

    secondnum = Integer.parseInt(secondnumET.getText().toString());
    

    如果留空,secondnumET.getText().toString() 将返回 null。

    这是完成您正在尝试的事情的更好方法。

    public void onClick(View v) {
    
    
        String firstnum = firstnumET.getText().toString();
        String secondnum = secondnumET.getText().toString();
    
        int cache1 = firstnum.equals("") ? 0 : Integer.parseInt(firstnum);
        int cache2 = secondnum.equals("") ? 0 : Integer.parseInt(secondnum);
        total = firstnum + secondnum;
    
        ansTV.setText(Integer.toString(total).length());  // set the answer to the length of total
        ansTV.setText(Integer.toString(total));  // set the answer to total
    }
    

    【讨论】:

    • firstnum 和 secondnum 永远不会等于 null。这不是 getText 的工作方式。解析 int 将返回字符串的值,而不是长度。在我的第一篇文章中自己犯了这个错误。
    • 你似乎是对的。我在应用程序中收到 NullPointerException 并添加对 null 的检查解决了它,但它一定是出于不同的原因解决了它。关闭以检查我的代码...
    • 以前肯定做过,编程的乐趣:P
    猜你喜欢
    • 2011-04-29
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多