• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Why will the || (or) operator not work with arrays

 
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
Me again,
This time i spent a while looking at my issue and thinking about the logic i thought i had nailed it but, it turns out java has other ideas;
it quite a simple program i am trying to build this time,
given 4 int args, print the number of duplicate entries, the catch is if 1 is entered four time then it is 1 duplicate.
however or operators don't seem to work with arrays (or maybe im using them wrong)
Here is my code;



Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '||'
 first type:  boolean
 second type: int
at variouscases.VariousCases.main(VariousCases.java:53)


any tips, Thanks
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each "thing" between the || needs to be a condition, or evaluate to a boolean, so

if that's what you mean.
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Each "thing" between the || needs to be a condition, or evaluate to a boolean, so

if that's what you mean.


Ah brilliant thank you,
So every time we say or it is  "a=b" or "a=c" and we cant do a = b orc .
As the second isn't a boolean, it become a statement as the or operand ended the previous boolean?
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you it now compiles but my logic is still not right, as even if all the args are different it still counts 1 each time it completely ignores the if statement seemingly,
or the array isnt adding the values in correctly, i was so sure of this one too
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also i realise now my logic is fundamentally flawed anyways, may i delete this thread??
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stef Aucky wrote:we cant do a = b or c .
As the second isn't a boolean, it become a statement as the or operand ended the previous boolean?


I'm not sure what you're trying to ask but what it becomes is nonsense that the compiler doesn't understand, hence the compile-time error.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stef Aucky wrote:also i realise now my logic is fundamentally flawed anyways, may i delete this thread??


We don't delete threads like this. Others coming in later might have the same flawed thinking and find the lesson you learned useful.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stef Aucky wrote:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '||'
 first type:  boolean
 second type: int
at variouscases.VariousCases.main(VariousCases.java:53)


any tips, Thanks


Here's a tip: Don't be fooled by your tool.

Even though that message says "RuntimeException," that's belied by the message "Uncompilable source code - ..." which means whatever it is your IDE is doing to make you think it's a RuntimeException that's causing the problem, it's really compile-time error that you need to address.
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well the change, that was suggested allowed it to compile.

but now i am having a similar issue to before with it seemingly ignoring the if statements.
the args i am using are 1 2 3 4
this is now my code I'm certain this should work (its not pretty though lol)


as you can see i have put in some print statements to see what is happening during execution,
the out put i get is

but it should be 0 the booleans should stop this as a[0] is not equal to a[1], a[2] or a[3],
yet it still increases the count of c
any ideas why?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look very carfully at your loop where you assign values into your array.

If you don't see the, then put in some more print statements between lines 8 and 9, printing out each value of a[0-3].


When you say this:  but it should be 0 the booleans should stop this as a[0] is not equal to a[1], a[2] or a[3], yet it still increases the count of c

think about that.  Like Sherlock Holmes said about elminating the impossible...you KNOW you actually ARE entering your IF-body because c is increasing. Therefore, something in your boolean expression must evaluate to true...
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:

a[0] -> 1
c : 1
a[1] -> 1
c : 2
a[2] -> 1
c : 2


probably you want to put the  line int b = 0; before the for loop.

 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Loganathan Karunakaran wrote:probably you want to put the  line int b = 0; before the for loop.


You gave it away!!!
 
Stan Austin
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loganathan Karunakaran thank you,
you did spoil the surprise but that dave me a whole heap of stress debugging haha,
i'm kicking myself that i didn't notice it the first through run through's

Fred Rosenburger thanks for the hint i would have figured it from there,
also what is the thing about printing the value of an array compared to printing the address i was told to watch out for doing this, but haven't actually got it wrong yet (even trying to) :/
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stan Austin wrote:also what is the thing about printing the value of an array compared to printing the address i was told to watch out for doing this, but haven't actually got it wrong yet (even trying to) :/


Well...it all depends on your needs. Sometimes you want to see the index, sometimes you want to see the value.  If you have a line like this:

a[i] = Integer.parseInt(args[b]);  

there may be a reason to print b, args[b], i, and a[i] (i.e. both indexes and both array elements).  
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic