Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using both String and Numerical variables in an If Statement  RSS feed

 
Mike Tompson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm attempting to check for valid input after the button is pressed using both string and numerical variables in my If Statements, but apparently they can't coexist or just require extra work.

I can check for a empty string field by using the following code, but I haven't figured out how to check for an empty numerical field:

But then I can't compare the numbers entered with < or > operators because I'm using a string variable, like so:

So, I thought would create a new temp variable using Integer.parseInt(), which would take the number from the original string variable and assign it to the new temp numerical variable, thus allowing me to use the compare operators. It worked, BUT, now my If Conditions using string variables quit working. It seems I can only use one or the other and I don't understand why.
I only created a new variable, and thus kept the original string variable, so why would my If Conditions that are based on string variables quit working?

What is the equivalent of the line below (string variable) to a numeric variable for empty fields, because that is all I need to get this to work?

if (mainColor.getText().toString().equals(""))

I tried the following but cannot get it to work like the string condition above, I always get an error:
if (numMainColor == "")
if (numMainColor == null)

Here is a portion of my code that contains the code in question:
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do is this:

First do all of the empty-field checks. Then if they all passed, convert the contents of the fields into numeric variables. Finally, do all of the checks where you compare those numeric variables to other numbers.

You keep referring to "numerical fields" but really that's a misnomer which is confusing you. All of those fields actually contain text. You may be expecting the user to type only digits into them, and you might even have used specialized fields which enforce that, but when you want to get the number out of them you have to explicitly convert the text to a number. (Which you already know how to do that.)

I also underlined something up above there. If you're going to check that a field isn't empty, and you find that it is empty, then sure, you should issue an error message. But you shouldn't then carry on and try to convert the empty field into a number, because then you'll get an exception. There's no point in that because it's entirely predictable that the exception is going to happen, and you already told the user there was a problem with the field.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!