• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean Function with Values not working  RSS feed

 
Alex Ruhl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am trying to develop a simple application getting a random number(i prefixed it so it will be always in the range i want) from a label and comparing it with a text field input if the text is GG for example
I am using eclipse and window builder plugin.


Main class


Random number generator and compare class




What am i missing?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Alex Ruhl wrote:What am i missing?
You slid at the same place as most people. Without knowing your application "brains" work as expected, you started on building GUI. Probably in the time being is better to discard your GUI part and concentrate on actual application.

Some points you need to address:
1. You need to improve your code indentation and formatting (1st thing to do, very important).
2. Variable names should start with a lower case (and follow camelCase, i.e. firstName), so we could distinct them from Class names.
3. You can't compare String contents with '==' sign (lines 41, 43). You need to use String method 'equals()'.

And when you said something isn't working, what you mean exactly, could you refer to the exact lines you have in mind and please provide error messages (if there are some, as I concluded it from your 'not working').
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also in the next coming your reply, please explain the intent of line 28, what it does? (from the bottom code).
 
Alex Ruhl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you a lot Liutauras Vilda its working now ,  i'm new to java i did similar project(larger one but with same ideas) in C# and it worked , i didn't know the == is not working for string comparison in java.

also ty for the 1) and 2) i will keep that in mind , have a nice day.
 
Alex Ruhl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Also in the next coming your reply, please explain the intent of line 28, what it does? (from the bottom code).


I need from the program to generate always specific numbers so it will be valid for me , ie if we have 1-10 random numbers i need it so its always will generate for example  3-5  7-8  and 10 , not 1-2 6 and 9 , if the "1-2 6 and 9" will be generated  it will be in the loop until it will generate the  "3-5  7-8  and 10" and exit the loop with the valid numbers for me .
The real application have 1000-99999 numbers and i need about 50 number ranges in there so it will be valid for the string comparison.

one string R from the actual program

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Ruhl wrote:I didn't know the == is not working for string comparison in java

It's not as clear cut as that:
This returns:

Match1
Match3

Why?

Match1: Fires because the "==" operator does work when comparing string literals (Constants) because of the String Pool used by Java. In this example we are not really comparing "Cat" with "Cat". We are comparing a location within the String pool. s1 and s2 will point to the same entry, therefore they must be the same. It's because of this String pool that String's can never change i.e. are immutable.

Match2: Will never fire because we are comparing a location on the String pool with an object ref on the heap. This will never match

Match3: Fires because the String pool entry for s1 matches the string value maintained within the String object instance s3.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Alex Ruhl wrote:I didn't know the == is not working for string comparison in java

It's not as clear cut as that:...

While it's good to know those things, as far as general programming practice is concerned, it actually is pretty clear-cut. Use equals() when checking for string/object equality. When dealing with object references, use == only for checking reference equality.

 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, are you saying you would advocate against using "==" for string comparisons, even if literals. Apologies if I'm over-complicating things for Alex.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Ruhl wrote:
I need from the program to generate always specific numbers so it will be valid for me , ie if we have 1-10 random numbers i need it so its always will generate for example  3-5  7-8  and 10 , not 1-2 6 and 9 , if the "1-2 6 and 9" will be generated  it will be in the loop until it will generate the  "3-5  7-8  and 10" and exit the loop with the valid numbers for me .
The real application have 1000-99999 numbers and i need about 50 number ranges in there so it will be valid for the string comparison.

That is pretty horrific -- you can avoid all that mess by using some code that you already wrote(?). Perhaps you don't understand what this line of code does:

First off, by convention, variable and parameter names in Java start with a lowercase letter, so that code really should be:

This line of code already limits the range of values that result can be assigned to (low..high], inclusive low, exclusive high. If you want to make it inclusive of high, you simply need to make a slight adjustment to the formula. I'll leave that to you to figure out.

So your loop to keep generating numbers until they are within a specific range is really unnecessary. All you have to do is use appropriate values for high and low and the proper formula will take care of things for you. What you should do is extract that line of code to its own method and parameterize it so that you can call it with different values of low and high.  This would be much better than writing 50 if-statements to handle each of the 50 number ranges you want to handle, which is quite frankly a horrific approach.

Lastly, your use of test == 1 and test == 0 appears to be a carry-over from C where it's common practice to use a 0 to indicate false and 1 to indicate true.  In Java, there is a boolean type, so this C practice is not appropriate and it makes your code less clear.  Use boolean true/false values instead for flags like your test variable.  And the name, test, is pretty meaningless. Something like inRange would have been a better, more expressive name that explains what its purpose is in the program.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:are you saying you would advocate against using "==" for string comparisons, even if literals.
What you'd win by that if your goal is to check if 2 strings are equal in content? Why to check for different thing in order to get answer you want?

No need to sorry, your previous post relieved some extra knowledge about the strings to the OP, which is good, but what Junilu is saying, you shouldn't use '==' for strings equality in practice, as it is not what this operator is doing. You may (and 100% you would) would see such question in one of Java certifications, so it is worth knowing after all.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:Junilu, are you saying you would advocate against using "==" for string comparisons, even if literals. Apologies if I'm over-complicating things for Alex.

Yes. Even though == can work out to have the same result as equals() in some cases, you should prefer clarity, consistency, and predictability in your code. Just because you know == is technically valid doesn't mean that other people reading your code know you're taking advantage of a technicality. If you mean to check for object equality, use the code that expresses that intent all the time. That would be the equals() method.

Edit: Just so we're clear and on the same page, since you wrote "for string comparisons". I'm saying for object/string equality, use equals(). For reference equality use ==. There are times when reference equality is appropriate. One of the best examples, ironically, is when implementing the equals() method:

The reference equality check is pretty much the first thing you do when overriding the equals() method. What that line is saying is "if other refers to this object".
 
Alex Ruhl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clements wrote:
Alex Ruhl wrote:I didn't know the == is not working for string comparison in java

It's not as clear cut as that:
This returns:

Match1
Match3

Why?

Match1: Fires because the "==" operator does work when comparing string literals (Constants) because of the String Pool used by Java. In this example we are not really comparing "Cat" with "Cat". We are comparing a location within the String pool. s1 and s2 will point to the same entry, therefore they must be the same. It's because of this String pool that String's can never change i.e. are immutable.

Match2: Will never fire because we are comparing a location on the String pool with an object ref on the heap. This will never match

Match3: Fires because the String pool entry for s1 matches the string value maintained within the String object instance s3.


I tried to run a small test before i post this topic , when i tested another string (not in the textfield but in code) String box ="GG"; the program worked . And i was like wtf why?

Ty for the comment i will put immutable reading on the to do list.


 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Yes. Even though == can work out to have the same result as equals() in some cases, you should prefer clarity, consistency, and predictability in your code. Just because you know == is technically valid doesn't mean that other people reading your code know you're taking advantage of a technicality.
I agree completely with what you're saying i.e. use what will always work, not something which will work some times and not others. Sound advice. I'll add it to my rapidly expanding Java coding standards list :-)
 
Alex Ruhl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Alex Ruhl wrote:
I need from the program to generate always specific numbers so it will be valid for me , ie if we have 1-10 random numbers i need it so its always will generate for example  3-5  7-8  and 10 , not 1-2 6 and 9 , if the "1-2 6 and 9" will be generated  it will be in the loop until it will generate the  "3-5  7-8  and 10" and exit the loop with the valid numbers for me .
The real application have 1000-99999 numbers and i need about 50 number ranges in there so it will be valid for the string comparison.

That is pretty horrific -- you can avoid all that mess by using some code that you already wrote(?). Perhaps you don't understand what this line of code does:

First off, by convention, variable and parameter names in Java start with a lowercase letter, so that code really should be:

This line of code already limits the range of values that result can be assigned to (low..high], inclusive low, exclusive high. If you want to make it inclusive of high, you simply need to make a slight adjustment to the formula. I'll leave that to you to figure out.

So your loop to keep generating numbers until they are within a specific range is really unnecessary. All you have to do is use appropriate values for high and low and the proper formula will take care of things for you. What you should do is extract that line of code to its own method and parameterize it so that you can call it with different values of low and high.  This would be much better than writing 50 if-statements to handle each of the 50 number ranges you want to handle, which is quite frankly a horrific approach.

Lastly, your use of test == 1 and test == 0 appears to be a carry-over from C where it's common practice to use a 0 to indicate false and 1 to indicate true.  In Java, there is a boolean type, so this C practice is not appropriate and it makes your code less clear.  Use boolean true/false values instead for flags like your test variable.  And the name, test, is pretty meaningless. Something like inRange would have been a better, more expressive name that explains what its purpose is in the program.


Now i get it , yes that low high code was copy paste because all other variables are low case(are mine) :P

All i want is to show in the label a Random number between 50-60 number ranges and the user(he got a cheat sheet to look for the right numbers and letters) will type some letters  lets say 0-10 ranges are A , 11-20 are B etc , the app is a test of the user skills .
I cant think a better way right now so solve this problem , i know its horrible codding .

I will put more thinking about your approach with your
extract that line of code to its own method and parameterize
but i dont know.... right know i am think it and don't like it ,maybe a variable with the end result of 50-60 methods calls with parameters of numbers range? and from all the methods end result must be also random because if i test lets say //97800-97999 R   give me a   978001 number and another method call with different ranges lets say //79370-80699 R   gives me the number 79371  again i must put all this end result and random them so the number that appear in the label will be always random and not static just from some methods.

Ty for your time and valuable comments  really appreciate it , i want to learn to code better.


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Ruhl wrote:and from all the methods end result must be also random because if i test lets say //97800-97999 R   give me a   978001 number and another method call with different ranges lets say //79370-80699 R   gives me the number 79371  again i must put all this end result and random them so the number that appear in the label will be always random and not static just from some methods.

I'm not sure if I understand you correctly but it seems like you might be mixing the idea of "randomness" with "uniqueness"  -- a sequence of random numbers is never guaranteed to have distinct/unique values. There's always a chance that you'll have repeats in a random number sequence.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing not to use == for.
Never write == true or == false, which are poor style and error‑prone.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!