It's not a secret anymore!*
The moose likes Beginning Java and the fly likes if one int is less than or greater than another int by one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if one int is less than or greater than another int by one" Watch "if one int is less than or greater than another int by one" New topic
Author

if one int is less than or greater than another int by one

Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
I'm trying to create a boolean that will return true/false based on if one int (which is a variable) is less than or greater than another int (which is a variable) by one. What I'm thinking of coding seems overly complicated and I was wondering if there was something that could do those calculations for me, like a method of some sort or a simple addition to a method. There's nothing in the int java docs that I can find.


Thanks.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Why don't you show us your code and perhaps we can take a look at it.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Sometimes once you have a method signature the implementation writes itself:



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
It can also be helpful to try a simpler problem first. For example, what if you ignore the "or greater than" part? Can you write a method that returns true if one int is less than the other by one? Hint: is there another way to write "less than the other by one"?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Don't overthink this problem, the solution is very very simple...

The text almost literally says it: check if the difference between two numbers is 1 or -1
Now you'll just have to convert that statement to Java code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: if one int is less than or greater than another int by one
 
Similar Threads
how to write: IF statement with range of values (e.g. if 5 > x > 10, then ... )
ArrayList/HashMap - Sorting
Sorting Names in SortNames
compareTo() Method of the String Class
difference between equal and compare methods of String class