title: How to get the length of an integer in Java (Android)
body: <p>So if I had the value 1234 the output should be 4.</p>
  
  <p>I've written the following code, but don't know what I did wrong.
  This is an App, that adds two numbers. So I've got two EditTextfields. But if an EditTextfield is left blank, the App crashes.
  So I thought of assigning the value of 0 to an EditTextfield, that has been left blank, because it (should) have a character length of 0.</p>
  
  <p>That should get the right results:</p>
  
  <p>Example, if EditTextfield "firstnumET" holds a value of 5 and "secondnumET" is left blank:</p>
  
  <p>5 + 0 = 5</p>
  
  <p>The problem might be </p>
  
  <hr>
  
  <pre><code>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));
      }
  }
  </code></pre>
  
  <hr>