# String \> Integer Conversion Problem

**URL:** <https://www.ruby-forum.com/t/string-integer-conversion-problem/50860>\
**Category:** Ruby\
**Created:** [December 21, 2005, 11:39pm UTC](https://www.ruby-forum.com/t/string-integer-conversion-problem/50860 "2005-12-21T23:39:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [December 21, 2005, 11:39pm UTC](https://www.ruby-forum.com/t/string-integer-conversion-problem/50860/1 "2005-12-21T23:39:01Z")

</div>

Retro thanks to all who helped me with my last post. I’m certainly more  
comfortable with Ruby now than then, but still a newbie as the following  
will surely demonstrate.

Below, you can see that I’m checking command-line arguments to the  
program for various conditions. Unfortunately, because the args are  
stored as strings, when I convert them via to\_i, empty and non-numerical  
strings become 0. 0 is an acceptable element in this program, therefore  
I can’t use it to test for invalid input. I’ve worked around this (sort  
of), by creating two sets of variables for the arguments (one set as  
strings, one set as integers). Unfortunately, this complicates the  
code, and more importantly, leaves me stumped concerning how to test for  
non-numeric values.

So, the program does what I want, except when the args are non-numeric  
strings, and the code seems uglier than it ought to be.

Thanks in advance,

-ELf

def gen\_chart(max)  
x=0  
y=0  
local\_chart = Array.new  
while x\<=max  
y+=x  
local\_chart[x]=y  
x+=1  
end  
local\_chart  
end

arg0 = ARGV[0]  
arg1 = ARGV[1]

arg0i = ARGV[0].to\_i  
arg1i = ARGV[1].to\_i

if arg0.nil? or (arg0i \< 0) or (arg1i \< 0)  
#print usage  
print \<\<-EOS

No args, or bad args!

Usage: #$0 [maxvalue]  
#$0 [minvalue] [maxvalue]  
EOS  
elsif arg1.nil?  
#do chart to arg0, print last pair  
chart = gen\_chart(arg0i)  
puts arg0i.to\_s + ": " + chart[arg0i].to\_s  
else  
#do chart to arg1, print pairs from arg0 to last  
chart = gen\_chart(arg1i)  
x=arg0i  
y=arg1i  
while x\<=y  
puts x.to\_s + ": " + chart[x].to\_s  
x+=1  
end  
end
