04-03-2012 09:01 AM - edited 03-14-2019 09:39 AM
I'm trying to put a piece of logic into a script that compares a customer entered value (string variable) against a static value (100000000). The logic I have looks like this:
If (valueEntered > "10000000") then
However almost any value entered returns a True result. So if I entered in 115, the script is seeing 115 as larger than 10000000 even though it is not.
I've tried to change the variable to an integer variable from string, but no luck. Anybody have any ideas on how to make this work so that only those values that are truely greater than 10000000 return a True result?
Solved! Go to Solution.
04-03-2012 09:11 AM
You are comparing against a string ("10000000" is a string). Try:
if (valueEntered > 10000000)
or
if (Integer.parseInt(valueEntered) > 10000000)
04-03-2012 09:13 AM
Hi,
no, you cannot compare apples to oranges, Strings to Integers.
You can, however, cast a String into Integer. It's easy.
Integer.parseInt(stringValue)
So, in your case:
If (Integer.parseInt(valueEntered) > 100000000)
etc.
http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29
G.
P.S. Integer is fine up to 2,147,483,647; for higher numbers use Long ~ (Long.parseLong(valueEntered)).
04-03-2012 09:11 AM
You are comparing against a string ("10000000" is a string). Try:
if (valueEntered > 10000000)
or
if (Integer.parseInt(valueEntered) > 10000000)
04-03-2012 09:13 AM
Hi,
no, you cannot compare apples to oranges, Strings to Integers.
You can, however, cast a String into Integer. It's easy.
Integer.parseInt(stringValue)
So, in your case:
If (Integer.parseInt(valueEntered) > 100000000)
etc.
http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29
G.
P.S. Integer is fine up to 2,147,483,647; for higher numbers use Long ~ (Long.parseLong(valueEntered)).
04-03-2012 10:59 AM
Thank you both!! I had to change it to int.parseInt but that works perfectly and is returning an accurate response.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide