programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Simplify if condition

Tiberius Marius
Ranch Hand
Posts: 115
3
I m trying to simplify an if condition as much as possible and i m curious if it can be made more short then i managed to .The problem is simple , a integer number with 3 digits is generated and the user is asked to input another 3 digits integer number ... One of the conditions that need to be verified is if all the digits in each number match but not exactly ( aka 123 and 132 ) .Using only an if statement to verify this what is the most short condition that can be checked to identify when the digits in a 3 digits number match the digits of another 3 digits integer number (not interested if they match exactly) .

The simplest condition i managed to find is :

Is there a better(shorter ) condition that can be used ?

Rico Felix
Ranch Hand
Posts: 411
5
This sort of problem can be made much simpler using some logic and the Java API

Here is one proposed way you can accomplish such a task:

Tiberius Marius
Ranch Hand
Posts: 115
3
Rico Felix wrote:This sort of problem can be made much simpler using some logic and the Java API

Here is one proposed way you can accomplish such a task:

I m using a book to learn and in that book this problem is meant to be resolved using only an if statement ( even for statement / char data type ware not introduced yet and i dont know allot about java arrays at my current study level) . Can you look for a shorter condition inside a simple if statement ?

Rico Felix
Ranch Hand
Posts: 411
5
Oh I see ... let me think

Paul Ngom
Ranch Hand
Posts: 355
1
try this

But i wonder if you will find it shorter

Tiberius Marius
Ranch Hand
Posts: 115
3
No strings

Tony Docherty
Bartender
Posts: 3271
82
It doesn't work anyway - if the guess is 222 and the lottery number is 123 it will return true.

A similar problem occurs in your solution Tiberius.

Paul Ngom
Ranch Hand
Posts: 355
1
You are perfectly right Tony. I think i needed to check the other way to like:

which makes it too long :-)

Is it correct now?

Winston Gutkowski
Bartender
Posts: 10575
66
Paul Ngom wrote:which makes it too long :-)

YES.

And that applies whether you write them yourself OR include them in a quote.

I've broken up all of yours THIS TIME. Any more, and you're on your own.

Winston

Paul Ngom
Ranch Hand
Posts: 355
1

DontWriteLongLines

Ok, Winston. I will try to stick to the rule next time.
Regards

Winston Gutkowski
Bartender
Posts: 10575
66
Tiberius Marius wrote:Using only an if statement to verify this what is the most short condition that can be checked to identify when the digits in a 3 digits number match the digits of another 3 digits integer number (not interested if they match exactly) .

This is a simple permutation problem, and doesn't jive with your other statement:
"I m trying to simplify an if condition as much as possible"

If you're NOT allowed to use anything other than an if statement involving the three separate digits, then you can't simplify it. The permutations are what they are, even if digits are duplicated (and checking for that, while it might make the logic work faster, is also likely to make it bigger).

There are, however, several ways of simplifying it if you CAN use other logic. My suggestion:
1. Put the digits in Sets (java.util.Set).
2. Check the lengths of those sets. If they're different, the digits can't possibly match.
3. (if needed) Put the digits in Lists (java.util.List), sort them, and check if they are identical. Hopefully, that one is self-explanatory.

Winston

 Actually, there is ONE check you could do (although it's an addition to all the others): check if the numbers that the digits come from are the same before you embark on your "digit permutation odyssey".

Paul Ngom
Ranch Hand
Posts: 355
1

There are, however, several ways of simplifying it if you CAN use other logic. My suggestion:
1. Put the digits in Sets (java.util.Set).
2. Check the lengths of those sets. If they're different, the digits can't possibly match.
3. Put the digits in Lists (java.util.List), sort them, and check if they are identical. Hopefully, that one is self-explanatory.

Winston, i am waiting to hear from Tiberius. I am almost sure he wouldn't like to use Sets ot Lists as he said that he has has just begun the language. By the way, could you post some code that achieves what you had said?

Tony Docherty
Bartender
Posts: 3271
82
You could simply do:

Tiberius Marius
Ranch Hand
Posts: 115
3
To avoid any more confusion i m going to post the whole exercise .Also remember that while it can be solved with strings/arrays or other objects the problem is in the third chapter and strings/ characters / objects / the for statement has not been introduced . What was introduced is the if statement and all the logical operators . My level of knowledge ends at objects (no knowledge of arrays /stacks because i didnt get that far yet.

*3.15 (from the book Introduction to Java Programming, Tenth Edition by Daniel Liang)
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a three-digit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:

1. If the user input matches the lottery number in the exact order, the award is \$10,000.

2. If all digits in the user input match all digits in the lottery number, the award is \$3,000.

3. If one digit in the user input matches a digit in the lottery number, the award is \$1,000

This is Lottery.java :

Tiberius Marius
Ranch Hand
Posts: 115
3
The correct solution(at list i think so ) that can verify the rule 2 is :

If you tell me it cannot be a more simple condition in the if statement i believe you !

Campbell Ritchie
Marshal
Posts: 56546
172
The existence of so many != operators entails a simpler solution in my opinion.
You are doing it wrongly by trying to solve the problem in code. What you should do is solve the problem on a sheet of paper. Write down exactly how you would solve it on paper, and then when you have got it nice and simple, you can turn it into code easily.

Paul Ngom
Ranch Hand
Posts: 355
1
And i will say that it is better that your if statement reads:

to avoid confusion

Tiberius Marius
Ranch Hand
Posts: 115
3
Paul Ngom wrote:And i will say that it is better that your if statement reads:

to avoid confusion

Think it is incorrect in the sense 111 and 100 will pass the condition ...

Paul Ngom
Ranch Hand
Posts: 355
1

Think it is incorrect in the sense 111 and 100 will pass the condition

Tiberius Marius
Ranch Hand
Posts: 115
3
This I think works but the one posted earlier by me is simpler i think ( the one with != )

Winston Gutkowski
Bartender
Posts: 10575
66
Paul Ngom wrote:And i will say that it is better that your if statement reads:

Paul,

After all my work, and my post (and your reply), you post ANOTHER piece of code that is FAR TOO LONG.

(Unlike you, I don't have 20:10 vision )

Winston

 And I notice that it has now been quoted by both of you.
If you simply want to swap replies, carry on; if you want to get answers from the people here:
EDIT YOUR POSTS AND BREAK UP THOSE LINES (and now you'll both have to do it).

Paul Ngom
Ranch Hand
Posts: 355
1

This I think works but the one posted earlier by me is simpler i think ( the one with != )

I think the one using != is not correct. You are also doing the same mistake.
You need to apply a ! to your if condition (like if(!expression)) for it to work.

Tiberius Marius
Ranch Hand
Posts: 115
3
Yes , your correct . Forgot to negate the whole condition .

Campbell Ritchie
Marshal
Posts: 56546
172
Pancil and paper. When you have got it worked out on paper, then you can easily translate it into code.

 It is sorta covered in the JavaRanch Style Guide.