0

I have readLine() which gives me the character. So, I have this:

223 345 567 and so on. So, when I convert them to Int first thing says these are character so, I researched and found this solution and when I use this:

let size    = readLine()
var array   = [Int]()
let numbers = readLine()

for number in numbers!
{
  if let integer = Int(String(number))
  {
      array.append(integer)
  }
}
print(array)

So, when I am printing the array, I am getting these as [2,2,3,3,4,5,5,6,7] instead of [223,345,567]. Can anyone help?

Here is the extract of new code.

let numbers = readLine()
var array   = [Int]()
guard let numberStrings = numbers?.components(separatedBy: " ") else{
fatalError()
}
for number in numberStrings {
if let validNumber = Int(number) {
    array.append(validNumber)
}
}

print(array)
0

2 Answers 2

1

You need to split the string and find every number string and then convert them to Int.

let numbers = readLine()
var numberArray: [Int] = []
guard let numberStrings = numbers?.components(separatedBy: " ") else {
    fatalError()
}

for number in numberStrings {
    if let validNumber = Int(number) {
        numberArray.append(validNumber)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting this error: value of type 'String?' has no member 'components' let strNumArray as the numbers = readLine() is giving me String. What to do about it?
Still same error. error: value of type 'String' has no member 'components'
@Rob13 can you share a screenshot of your xcode showing that error along with the code?
0
for number in numbers!

This line of code extracts each character in the string (223 345 567) and tries to convert it to int. That's why it converts each valid number in string and left the spaces. You need to first split the string in to array of string numbers then iterate through the array to convert them.

Split it, further iterate through strNumArray and convert them to integers

var array   = [Int]()
if let strNumArray = numbers?.components(separatedBy: " ") {
    for number in strNumArray {
        if let integer = Int(String(number)) {
            array.append(integer)
        }
    }
}
print(array)

And in more swifty way

var arrayInt = numbers.split(separator: " ").map{Int($0)}

13 Comments

I am getting this error: value of type 'String?' has no member 'components' let strNumArray as the numbers = readLine() is giving me String. What to do about it?
if 'numbers' is optional use if let or guard
@Rob13 adding guard or if let helped you ?
Nope, same error. I just tried this more swifty way and it works. Why didn't it worked with the above code? I am using Swift 4.1, is that the issue?
I had updated the answer yesterday. Please see if this works for you.
|

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.