• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to do a number range in if statement in Java?  RSS feed

 
Christopher thompson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For example: if(JTextField = 15-30){
do this
}

I know it's simple but i have no clue how it's done lol, any help would be appereciated
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't get a text field to equal a number. You can however get a number to equal another number. You are probably best using integer arithmetic for that sort of thing because floating point arithmetic counts less well than my little grandsons. If you don't believe me, run the program in this post.

You can tell whether a number is less than another, or more than another, but not both together. You have to use the AND operator (you usually use the two‑keystroke version).
You can tell whether i is less than 100 like this
i < 100
You can tell whether i is greater than 0 like this
i > 0
So you can tell both like this
i > 0 && i < 100
Or like this
0 < i && i < 100
if you like having the arrowheads the same way round. You cannot write 0 < i < 100 however.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, wrote 0 < 1 earlier. Have corrected the 1 to i.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you first have to convert the contents of this jTextField to some numbers.
Is it allowed to use a single number as well? Like when you have a printer dialog,
where you can put the pages to print as, say, 1,2,3, 8-18, ....

So, what is possible?

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the best of my knowledge, no, you can't use arbitrary lists of values like that. You are not passing arguments, but writing an expression. I can imagine writing a method with varargs where you can pass any number:
public void printPages(int... pageNumbers)…
Using a hyphen for a range would be more difficult because it would be interpreted as a minus. You could pass "1,2,3, 8-18, 26" as a String however, split it on the commas, and parse the ranges. Then you put the values into a List and print every page on the List. Or put them into a sorted set which obviates duplicates. But you can't (as far as I know) use ranges like that in ordinary expressions.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if we're dealing with a JTextField, you can easily type such expressions.
Therefore I asked what possible inputs can be expected. OP's information is
not very comprehensive.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to parse that sort of expression as I said, adding single numbers to a set and using the ranges 8‑18 as ranges 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18.
Then return mySet.contains(i);
I can see all sorts of exceptions if you use ranges like 18‑8!
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's wait for Christopher before letting our fantasy taking over.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's never stopped us before
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it won't stop us in the future...
 
Justin Musgrove
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using java 8 or guava here are a few code snippets to check if a number is within a range:

Guava


Java 8
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice examples, but please provide a bit of explanation for them, or at least the J8 version. The link you posted was pretty short on explanations.
Do you really want AssertXXX there? It is not as if you were going to throw an Exception if the value is absent.
Maybe you simply want
return containsValue.isPresent();
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!