Home > Software engineering >  How can I compare two different types of data (String and Int) using readLine() on Swift?
How can I compare two different types of data (String and Int) using readLine() on Swift?

Time:11-19

everyone! I'm a new member of Stack Overflow, just like I'm beginner on swift programming. I'm making this post to find out a solution for the following case:

I'm creating on Swift an app using the Command Line Tool for inputing data. The app basically works as an authenticator. As an example, if someone types USA for the country name and the age is 17, so the program will return a message like "You can not apply to this position". Otherwise, if the country name is USA and the age is equal or higher than 18, so the message returns is "You can forward to the next step". I've tried many times to set this conditions, but it's not working. I'm already knows that the function readLine() is an Optional String, but how can I make my program work correctly? It follows my code above to you understanding my thoughts.

I really appreciate any help. Again, I'm beginner and I'm already studying Swift languages, but I'm seeking some solution that handles Integers and Strings and comparing both data types. Thank you very much!

My code is:

import Foundation

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var country, var age = readLine(){
    if country == "USA" && age < "18" {
        print("You're not allowed to apply to this position.")
    } else {
        print("You can forward to the next step.")
    }
    
}


PS: As you see, I'm using wrongly the variable age as an String, but I want to convert it to an Int type and then, check if the country name is the same than the value I assigned to or the age is equal or higher than 18. But not found a solution so far.

I'm trying to find a solution that compares two different types on Swift, using Command Line Tool and the readLine() function to check if a condition is true or not. If it's true, an output message will show that the user can proceed to the next step, otherwise he will not be permitted to follow. I'm keeping for an explanation on internet since few days, but not found anything that might help me. I hope to get some help using the Stack Overflow forum to some useful answer.

CodePudding user response:

Firstly, welcome to Stackoverflow

So I will split your problem into smaller parts

First one, readline() means you read the current to the end of current line . As your code when you check condition you call readLine() again. The wrong part is here.

I will go step by step

So as new developer, I recommend you to read first then do all your logic. You just need to read only one time at first

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

Next, check if it is nil or not ( because option value which is value can be nil)

if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

Then check if can convert to Int or not. Reach here you sure that the ageString is not nil because you have checked above. So you just simply convert

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

Then after all, you just check your condition

Full code will be like this

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

if country == "USA" && age < 18 {
    print("You're not allowed to apply to this position.")
} else {
    print("You can forward to the next step.")
}

Other methods is use if let to achieve so no force unwrap

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

if let ageString = ageString {
    if let age = Int(ageString) {
        if country == "USA" && age < 18 {
            print("You're not allowed to apply to this position.")
        } else {
            print("You can forward to the next step.")
        }
    } else {
        print("Error age not a number")
        fatalError()
    }
}

CodePudding user response:

I hope this help you :)

import Foundation

print("Enter your country: ")
if let country = readLine() {
  if let num = Int(country) {
      print(num)
      }
    }
      let country = readLine()
let age = readLine()


if let USA = country, 
    let num1 = Int(USA), 
    let num2 = Int(USA) {
    print("The age of \(num1) and \(num2) is \(num1   num2)")
}
  • Related