-2

I have a variable assigned to user input. I want to check if the user input is an integer. how do I do that?

a = gets
sawa
  • 165,429
  • 45
  • 277
  • 381

1 Answers1

3

There is no need to check: gets returns a String, so a will never reference an Integer, it will always be a String. There is no possible way that a will ever reference an Integer in the code you posted.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • I think, *never* is a bit too strong. The method may have been redefined, or the context is executed inside a class where `gets` doesn't necessarily return a `String`.... – user1934428 Jun 04 '18 at 04:56
  • @user1934428: "in the code you posted". In that code, "never" is the correct estimation. – Sergio Tulentsev Jun 04 '18 at 18:29
  • 1
    @SergioTulentsev : Right you are. From the way the OP wrote it, we must assume that this statement is not just a line picked from a large application, and in this case, *never* is indeed correct. I didn't consider that this might just be the first line of a complete application. Thanks for having me corrected. – user1934428 Jun 05 '18 at 05:43