This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Boolean Problem  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I'm trying to write a program that gives the biggest of 3 numbers (integers) and this is the error I get "operator > cannot be applied to boolean, int" Could someone tell me what am I doing wrong? cuz a similar code works for just 2 numbers.
Here's the code

import acm.program.*;

public class ProgramaMayorDe3 extends DialogProgram {

public static void main(String[] args) {
new ProgramaMayorDe3().start();
}

public void run() {

int x;
int y;
int z;

x = this.readInt("Dame el primer numero:");
y = this.readInt("Dame el segundo numero:");
z = this.readInt("Dame el tercer numero:");

int r = this.mayor(x, y, z);

this.println("El mayor entre " + x + " , " + y + " y " + z + " es: " + r);
}

public int mayor(int a, int b, int c) {
if (a > b > c) {
return a;
} if (a < b > c) {
return b;
} else {
return c;
}

}
}
 
Sheriff
Posts: 9094
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a > b > c is not a java expression. You need something like


or


By the way, WillowR, welcome to JavaRanch. Please take a moment and adjust your display name to meet the JavaRanch Naming Policy.
You can change it here.

Thanks!
[ October 15, 2005: Message edited by: Marilyn de Queiroz ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with (a > b > c) is that a > b evaluates to a boolean (true or false), and then you have a problem because you're trying to determine if this boolean is "greater than" c (which is an int). You can't compare booleans to ints.

Basically, you'll want to rewrite this to something like...

(a > b) && (b > c)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think for a minute how you'd solve this without a computer. If I gave you a written list of numbers just big enough that you couldn't spot the biggest one easily you might look at the first and say "that's the biggest so far", then look at the next and see if it's bigger, then the next. When you find a new "biggest so far" you might put your finger there to hold the place.

I'm thinking like a mean spirited teacher ... what if your instructor says "Now you've solved for three, do it for ten" how could you make the program handle more input numbers without getting longer and longer combinations of x > a && x > b ...
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!