Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

Adding up the numbers in a String java

I am trying to add up the characters in a String. But that doesn't seem to work. I am using this code:

public String createDeciOfOct (String number) {
    int result = 0;
    String[] numberArray = number.split("");
    for (String numb : numberArray) {
        if(numb != null || numb != "") {
            result += Integer.parseInt(numb);
        }
    }
    return Integer.toString(result);
}

How I am executing it is like this:

frmOctToDeci.setOnClickListener(new View.OnClickListener() {            
  @Override
  public void onClick(View v) {
    EditText edtTxt = (EditText) findViewById(R.id.octalValue);
    String value = edtTxt.getText().toString();
    String result = createDeciOfOct(value);
    TextView txtView = (TextView) findViewById(R.id.fromOctRes);
    txtView.setText(result);
  }
});

It gives me an error as:

java.lang.NumberFormatException: Invalid int: ""

Which means that there is some sort of empty string. How can I prevent this?

The logcat errors are as:

enter image description here

Upvotes: 1

Views: 150

Answers (4)

pdem
pdem

Reputation: 4076

That is wrong, so i edited it.

        if(numb != null || numb != "") 

it should be (i thought, so it's still wrong)

        if(numb != null && numb != "")

Edit: the comment is right, i just wanted to notice the && instead of || . nevermind, forget it !

so it should really be:

        if(numb != null && ! numb .equals ("") )

of course ! "".equals(numb) does the same

Upvotes: 1

Bhavit S. Sengar
Bhavit S. Sengar

Reputation: 8934

This algorithm will work:

public String createDeciOfOct (String number) {
    int result = 0;

    for (int i = 0 ; i<number.length(); i++) {
        result = result + Integer.parseInt(String.valueOf(number.charAt(i)));
    }
    return Integer.toString(result);
}

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236170

Always use equals() for testing (in)equality between Strings:

!numb.equals("")

Also it might be easier to process the input as a char[], but that's just an idea:

char[] numberArray = number.toCharArray()

Then it's easy to convert a char to a digit, simply use Character.getNumericValue() on each char you wish to convert.

Upvotes: 5

Pierre Rust
Pierre Rust

Reputation: 2504

First, as another answer state it, you should not use != to compare strings, but equals.

Then, split takes a regexp and is a weird way of converting a String into an array of one-character string. I think it will be better to simply take the corresponding char array and use Character.getNumericValue

char[] charArray = number.toCharArray();
for (char c: charArray ) {
    result += Character.getNumericValue(numb);
}

Be carrefull, the code above does not perform any check to make sure your initial string contains only digit characters. If there are such characters the code will not fail but will produce unexpected result.

Upvotes: 0

Related Questions