• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with if statements  RSS feed

 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to do a simple if statement.
If 1 is in "phoneno", Valid number should come up, if not, invalid number should come up..

At the minute, no matter what number is pressed, invalid number comes up..

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liam,
Welcome to CodeRanch!

You've hit a common beginner mistake. == is only used to compare primitives (like int). For String, you use equals():

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you're wondering, testing the constant instead of the variable is a good habit to get into. It avoids NullPointerExceptions (NPEs).
 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne. That helped and got me going.

What I want to do now is check if "phone" has just three digits between 0-9 in it, using regex..
This is what I'm trying, but getting an error of illegal start to experession..

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things: you need to use the matches method, and you need to quote the regex:

Also, matches matches the whole string, so "$" and "^" are unnecessary.
 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, got it working, deadly!! Thanks Knute!!
My main aim here is to validate phone numbers, but i figured just getting this basic code working first was my best bet.
Hopefully I can solve it from here, if not, ill be back..
Thanks again!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!