• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using both String and Numerical variables in an If Statement

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic