1

I want to add first and second value of editText. For example: If user enters 15 then add 1 into 5. I have tried this code:

    val input = editText.text
    if (input.length == 2) { 
        val firstValue = input.length == 1 
        val secondValue = input.length == 2 
        val result = firstValue + secondValue
        textView.text = result
    }

but I get this result: "[Ljava.lang.Boolean;@312b186"

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • what would `input.length == 1` indicate? – KevinO Nov 13 '18 at 12:30
  • if you have a line of `if (input.length == 2)` which returns a boolean, why would `input.length == 1` therefore return the first input? – KevinO Nov 13 '18 at 12:34
  • 2
    this question is in kotlin. – user10646020 Nov 13 '18 at 12:36
  • 1
    Honestly: I think you are trying to do 10 things at the same time, but you understand only 10% of the code you write. I think you are overburdening yourself right now, and you should step back and study some basic kotlin for a while. There is no point in trying to do "real world" things when you have no idea how the language works. Beyond that, you got some good answers by now, so please dont forget about accepting one of them! – GhostCat Nov 13 '18 at 12:43

6 Answers6

1
    val firstValue = input.length == 1 
    val secondValue = input.length == 2 

Both of these blocks are actually booleans. firstValue is the boolean result of "is input length equal to 1", and similarly for secondValue. This means firstValue + secondValue is actually something like

val result = true + false

which I guess isn't being picked up as an error at compile time because of the Java 9 val keyword. But result is actually a boolean.

Ben R.
  • 1,965
  • 1
  • 13
  • 23
1

First of all, this is "half of" a duplicate of this question. But it goes beyond that.

The problem here is that val is hiding actual types, and then things go wrong.

[Ljava.lang.Boolean;@312b186" means that you are calling toString() on an array of Boolean objects.

In other words: instead of putting together a string object, you created a boolean array (with two values), and then you get thatArray.toString() as result.

So, in a first step: don't use val all over the place, but give distinct types.

It looks like you expect the incoming strings to be numbers. If so, you should rather do as the other answer suggests, and use toInt() on your input strings!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

The issue is that val firstValue = input.length == 1 returns a boolean. As is somewhat clear by the original if test.

To get the value of the position, first you will need to do something like:

val firstValue = input.get(0);

However, I would expect that to return a String. So you will need to convert the String to an integer.

val firstValue = input.get(0).toInt(10);

You should then be able to add the values (subject to making modifications to the secondValue).

Note: instead of get(), it might be necessary to do .subSequence(0, 1) and .subsequence(1, 2) depending upon the specifics of the .toInt(). I don't have a Kotlin environment running at the moment to confirm.

KevinO
  • 4,303
  • 4
  • 27
  • 36
  • 1
    Ah, I was about to change my answer to talk about "how to convert to int". I took the short path, just mention it, and upvoted your example instead ;-) – GhostCat Nov 13 '18 at 12:41
  • You're right about having to use `subsequence`, `toInt` on a CharSequence won't compile. Another small note, semicolons are optional – Eamon Scullion Nov 13 '18 at 12:52
1

In your example, you are incorrectly retrieving the values from the EditText:

val firstValue = input.length == 1

What this will do is check if input.length has a length of 1, and will set firstValue to the resulting boolean (either true or false). hence why you are a Boolean as a result.

To retrieve text from an EditText, you must do something like:

val inputString = input.getText().toString()

Which will retrieve the text from an EditText as a String.

Next, we retrieve a single character using substring, then parse it to an integer.

val firstValue = inputString.substring(0,1).toInt()
val secondValue = inputString.subString(1,2).toInt()

We can now add our integers together to get the result:

val result = firstValue + secondValue
Eamon Scullion
  • 1,354
  • 7
  • 16
1

firstValue and secondValue are assigned booleans and not Ints. A quick fix will be as follows.

val input = editText.text.toString()
if (input.length == 2) { 
    val firstValue = input[0]
    val secondValue = input[1]
    val result = firstValue.toInt() + secondValue.toInt()
    textView.text = "$result"
}`
ngenge
  • 157
  • 1
  • 8
0

Extract the remainder of the number by using modulus operation and add the remainder to the result variable. Here's the code...

    int remainder, result = 0;
    String value= editText.getText().toString();
    int i=Integer.parseInt(value);
    int temp=i;
    while(temp != 0){
        remainder=temp % 10;
        result += remainder;
        temp=temp/10;
    }
  • I don't see how this answer addresses the issue of the OP. They have (in essence) a `String`, and this answer starts with the assumption of an `int`. If the OP first converted the `String` to an `int`, then this approach could work. – KevinO Nov 13 '18 at 12:47
  • The question is asking for Kotlin, this won't compile – Eamon Scullion Nov 13 '18 at 12:54
  • @KevinO You're right. I just gave him the logic of how to do it. Obviously for his requirement, editText content should be converted to int. I probably should've mentioned that in the answer. – Cibi Pranov A Nov 13 '18 at 12:55