1

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

4
  • 1
    What you expect to do when you split with "" and you compare strings with equals , == is just for references Commented Feb 26, 2014 at 15:38
  • @nachokk, I want to skip it when there is no integer value there Commented Feb 26, 2014 at 15:39
  • 1
    Use equals when comparing strings Commented Feb 26, 2014 at 15:39
  • and a split with no split character... what does it lead to? Commented Feb 26, 2014 at 15:40

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

A nice a great solution, in both aspects! :) char[] would also be a great idea! :) However, I am a bit new to Java API that's why I am facing these troubles...
1

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

2 Comments

No it should not :) When comparing strings, use equals(), not ==
how i could not see that basic thing i will edit, but let mi first mistake for the future!
0

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.

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.