By ibroadsword


2017-12-01 11:13:17 8 Comments

So I have a String of integers that looks like "82389235", but I wanted to iterate through it to add each number individually to a MutableList. However, when I go about it the way I think it would be handled:

var text = "82389235"

for (num in text) numbers.add(num.toInt())

This adds numbers completely unrelated to the string to the list. Yet, if I use println to output it to the console it iterates through the string perfectly fine.

How do I properly convert a Char to an Int?

3 comments

@s1m0nw1 2017-12-01 11:22:16

That's because num is a Char, i.e. the resulting values are the ascii value of that char.

This will do the trick:

val txt = "82389235"
val numbers = txt.map { it.toString().toInt() }

The map could be further simplified:

map(Character::getNumericValue)

@ibroadsword 2017-12-01 11:37:33

Thank you. I was extremely confused.

@Vadzim 2017-12-08 12:24:25

On JVM there is efficient java.lang.Character.getNumericValue() available:

val numbers: List<Int> = "82389235".map(Character::getNumericValue)

@Moira 2017-12-08 14:19:55

You seem to have linked to the wrong answer on that question, I've changed it if you don't mind

@zsmb13 2017-12-01 11:25:28

The variable num is of type Char. Calling toInt() on this returns its ASCII code, and that's what you're appending to the list.

If you want to append the numerical value, you can just subtract the ASCII code of 0 from each digit:

numbers.add(num.toInt() - '0'.toInt())

Which is a bit nicer like this:

val zeroAscii = '0'.toInt()
for(num in text) {
    numbers.add(num.toInt() - zeroAscii)
}

This works with a map operation too, so that you don't have to create a MutableList at all:

val zeroAscii = '0'.toInt()
val numbers = text.map { it.toInt() - zeroAscii }

Alternatively, you could convert each character individually to a String, since String.toInt() actually parses the number - this seems a bit wasteful in terms of the objects created though:

numbers.add(num.toString().toInt())

@zsmb13 2020-01-01 15:38:39

'0'.toInt() correctly returns 48, which as explained in the answer, is the ASCII code for the character '0'. If you want the numerical value of a character containing a digit, you'll have to subtract the ASCII value of '0' from it.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

40 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

17 Answered Questions

[SOLVED] How to clone or copy a list?

27 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray
  • 2730190 View
  • 3234 Score
  • 27 Answer
  • Tags:   python list

7 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3224775 View
  • 1905 Score
  • 7 Answer
  • Tags:   python list

15 Answered Questions

[SOLVED] Convert two lists into a dictionary

62 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

13 Answered Questions

[SOLVED] How to randomly select an item from a list?

  • 2008-11-20 18:42:21
  • Ray
  • 1369641 View
  • 1714 Score
  • 13 Answer
  • Tags:   python list random

19 Answered Questions

[SOLVED] Converting array to list in Java

27 Answered Questions

[SOLVED] Convert a string to an integer?

Sponsored Content