Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Bad operand types for binary operator '<' error issue (beginner)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bad operand types for binary operator '<' error issue (beginner)

    Okay so my problem occurs when I am trying to compare numbers that I have sent into method. In all six cases, I get "error: bad operand types for binary operator '<' ", and is always pointing at the same "<" symbol.

    Say the expression a<b<c, the error always occurs with the "<" between b and c.

    Any help is greatly appreciated


     
    import java.io.*;
    import java.util.*;
     
    public class Program1
    {
    	public static void main( String args[])
    	{
    	int user_guess, com_guess, gameover=0;
    	Scanner scan = new Scanner(System.in);
    	Random rand = new Random();
     
    	System.out.println("Hello User...I want to play a game.\nA game you cannot possibly win\n\nEnter the lower bound: ");
    	int lower= scan.nextInt();
    	System.out.println("Enter the upper bound: ");
    	int upper= scan.nextInt();
     
    	if (lower>=upper)
    	{
    	System.out.println("\nBad");
    	System.exit(0);
    	}
     
    	int range=upper-lower;
    	int magic_number=rand.nextInt(range)+lower;
     
    	System.out.println("\nLET THE GAME BEGIN. MUHAHAHAHAH!!!\n\nThe magic number is between "+lower+" and "+upper+" inclusively" );
     
    	while(gameover<1)
    	{
    	System.out.println("Enter your guess: ");
    	user_guess=scan.nextInt();
     
    	com_guess=(upper+lower)/2;
    	System.out.println("\nMy guess is: "+com_guess);
     
     
    	if (user_guess<magic_number)
    	{
    	System.out.println("User guess is: LOW");
    	lower= user_adjust(lower, user_guess, magic_number);
    	}
    	else if (user_guess>magic_number)
    	{
    	System.out.println("User guess is: HIGH");
    	upper= user_adjust(upper, user_guess, magic_number);
    	}
    	else if (user_guess==magic_number)
    	{
    	System.out.println("IMPOSSIBLE!!! You win this round...");
    	gameover=1;
    	}
     
    	if (user_guess<magic_number)
    	{
    	System.out.println("My guess is: LOW");
    	lower= com_adjust(lower, com_guess, magic_number);
    	}
    	else if (com_guess>magic_number)
    	{
    	System.out.println("My guess is: HIGH");
    	upper= com_adjust(upper, com_guess, magic_number);
    	}
    	else if (com_guess==magic_number)
    	{
    	System.out.println("Aaaaand boom goes the dynamite. You lose.");
    	gameover=1;
    	}
     
    	}
     
    	System.out.println("THANKS FOR PLAYING. COME BACK SOON");
     
    	}
     
     
    //*******************************************************************************************
    //                  This is where the methods start
     
     
     
    	private static int user_adjust(int LU, int use, int ans)
    	{
    	if (LU < use < ans)
    	{
    	LU=use+1;
    	return LU;
    	}
    	else if (ans<use<LU)
    	{
    	LU=use-1;
    	return LU;
    	}
    	else if (use<LU<ans)
    	System.out.println("\nBlah\n");
    	else if (ans<LU<use)
    	System.out.println("\nBlah\n");
    	}
     
    	private static int com_adjust(int LU, int com, int ans)
    	{
    	if (LU<com<ans)
    	{
    	LU=com+1;
    	return LU;
    	}
    	else if (ans<com<LU)
    	{
    	LU=com-1;
    	return LU;
    	}
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bad operand types for binary operator '<' error issue (beginner)

    **Update**

    I just added the following code to my main program, just to see if I would get the same error.

    	if (1<2<3)
    	System.out.println("Yay");

    I got the exact same error, so I'm betting whatever my problem is, it is something very basic

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Bad operand types for binary operator '<' error issue (beginner)

    You can't write 1<2<3 as you would in mathematics. You have to spell out what you mean:

    if(1 < 2 && 2 < 3) {
        System.out.println("Yay");
    }

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bad operand types for binary operator '<' error issue (beginner)

    Thanks a lot

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Bad operand types for binary operator '<' error issue (beginner)

    You're welcome.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    My Mood
    Inspired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bad operand types for binary operator '<' error issue (beginner)

    Hey pbrockway2, thanks for you help too!

    I've been following you and mju516 and I had the same problem too, which I fixed (below)...

    import java.util.Scanner; // program uses class scanner

    public class ThreeIntegerSumAvgProductSmallLarge
    {
    public static void main ( String args [] )
    {
    Scanner consoleScanner = new Scanner ( System.in );

    int number1,
    number2,
    number3,

    sum,
    average,
    product,

    smallest = 0,
    largest = 0;

    System.out.print ( "Please enter 1st integer: " );
    number1 = consoleScanner.nextInt ();

    System.out.print ( "\nPlease enter 2nd integer: \n" );
    number2 = consoleScanner.nextInt ();

    System.out.print ( "Please enter 3rd integer: \n" );
    number3 = consoleScanner.nextInt ();

    sum = number1 + number2 + number3;
    average = ( ( number1 * number2 * number3 ) / 3 );
    product = number1 * number2 * number3;

    if ( number1 > number2 && number2 > number3 )
    largest = number1;
    if ( number2 > number1 && number1 > number3 )
    largest = number2;
    if ( number3 > number2 && number2 > number1 )
    largest = number3;

    if ( number1 < number2 && number2 < number3 )
    smallest = number1;
    if ( number2 < number1 && number1 < number3 )
    smallest = number2;
    if ( number3 < number2 && number2 < number1 )
    smallest = number3;

    System.out.printf ( "%s%d\n", "The sum of your three numbers is: ", sum );
    System.out.printf ( "%s%d\n", "The product of your three numbers is: ", product );
    System.out.printf ( "%s%d\n", "Your three numbers produced an average of: ", average );

    System.out.printf ( "\n%s%d\n", "The largest number out of the three is: ", largest );
    System.out.printf ( "%s%d\n", "The smallest number out of the three is: ", smallest );
    }






    However, now I'm trying to compare five numbers and print out the largest and the smallest (just like the one above), but now it's not working (below)....

    import java.util.Scanner; // Program uses the Scanner class

    public class Exercise2_24
    {
    public static void main ( String args [] )
    {
    Scanner numberScanner = new Scanner ( System.in );

    int int1,
    int2,
    int3,
    int4,
    int5,

    big = 0,
    small = 0;

    System.out.print ( "Please enter the first integer: " );
    int1 = numberScanner.nextInt ();

    System.out.print ( "\nPlease enter the second integer: " );
    int2 = numberScanner.nextInt ();

    System.out.print ( "\nPlease enter the third integer: " );
    int3 = numberScanner.nextInt ();

    System.out.print ( "\nPlease enter the fourth integer: " );
    int4 = numberScanner.nextInt ();

    System.out.print ( "\nPlease enter the fifth integer: " );
    int5 = numberScanner.nextInt ();

    if ( int1 > int2 && int2 > int3 && int2 > int4 && int2 > int5 ) big = int1;
    if ( int2 > int3 && int3 > int4 && int3 > int5 && int3 > int1 ) big = int2;
    if ( int3 > int4 && int4 > int5 && int4 > int1 && int4 > int2 ) big = int3;
    if ( int4 > int5 && int5 > int1 && int5 > int2 && int5 > int3 ) big = int4;
    if ( int5 > int1 && int1 > int2 && int1 > int3 && int1 > int4 ) big = int5;

    if ( int1 < int2 && int2 < int3 && int3 < int4 && int4 < int5 ) small = int1;
    if ( int2 < int3 && int3 < int4 && int4 < int5 && int5 < int1 ) small = int2;
    if ( int3 < int4 && int4 < int5 && int5 < int1 && int1 < int2 ) small = int3;
    if ( int4 < int5 && int5 < int1 && int1 < int2 && int2 < int3 ) small = int4;
    if ( int5 < int1 && int1 < int2 && int2 < int3 && int3 < int4 ) small = int5;

    System.out.printf ( "\nThe largest number entered is: %d", big );
    System.out.printf ( "\nThe smallest number entered is: %d", small );
    }
    }

    I compared this incorrect code with the one that works correctly (the 3 number one)....I don't see any noticeable errors. I'm thinking that I'm incorrect with how I "spelled out" what I meant in comparing the 5 five numbers in order to obtain the biggest and largest number, and I even changed that up in the if statements for the 'big variable, but that didn't work.

    There's no compile time errors, it's run time, and most times I run the program, I don't get the correct big and small numbers to print out. Sometimes I get just a 0 for either small or large number that is printed out. It's like after the 'big' and 'small' variables are initialized to 0, the if statements are not assignments any numbers to them.

    Please help...thanks!

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Bad operand types for binary operator '<' error issue (beginner)

    if ( int1 > int2 && int2 > int3 && int2 > int4 && int2 > int5 ) big = int1;
    Think what it means for int1 to be the biggest. You are comparing int2 with int3 etc. But none of those comparisons matter. What matters is this: int1 should be bigger than everything else - bigger than int2, bigger than int3 etc.

    Try to translate "bigger than everything else" into a condition with "ands".

    -----

    Let's hope you don't need to find the biggest of 42 things or you might want to rethink the approach slightly! But for now you should be able to get the right condition for each of the if statements.

    Also you might want to look at the values of the average that are being produced.

    -----

    It would have been a good idea to start a new thread for your question - even if this one helped you along your way. What I'm thinking is that people will see that the thread is marked "solved" and may well not bother to read it.
    Last edited by pbrockway2; January 27th, 2012 at 04:03 AM.

  8. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    My Mood
    Inspired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Talking Re: Bad operand types for binary operator '<' error issue (beginner)

    Oops! Thanks for finding the bug in the average. I guess I should've tested with more numbers other than 1, 2, and 3 since both the sum and product would be the same. I posted the corrections below...

    As the other code, I've noticed several ways to make it work (like with counters and arrays); however, the workbook that I'm getting the exercise from stated to only use what was learned from the chapter...and I tried with using the &&s again, like..

    if ( int1 > int2 && int1 > int3 && int1 > int4 && int1 > int5 ) big = int1;

    but that didn't work...fortunately, I did find the answer after more searching...and I'm gonna post it in a new thread as you suggested...although, I would like to see how you can do the same thing with the ands...thanks again!

    import java.util.Scanner; // program uses class scanner
     
    public class ThreeIntegerSumAvgProductSmallLarge
    {
    	public static void main ( String args [] )
    	{
    		Scanner consoleScanner = new Scanner ( System.in );
     
    		int number1,
    			number2,
    			number3,
     
    			sum,
    			average,
    			product,
     
    			smallest = 0,
    			largest = 0;
     
    		System.out.print ( "Please enter 1st integer: " );
    			number1 = consoleScanner.nextInt ();
     
    		System.out.print ( "\nPlease enter 2nd integer: \n" );
    			number2 = consoleScanner.nextInt ();
     
    		System.out.print ( "Please enter 3rd integer: \n" );
    			number3 = consoleScanner.nextInt ();
     
    		sum = number1 + number2 + number3;
    		average = ( ( number1 + number2 + number3 ) / 3 );
    		product =  number1 * number2 * number3;
     
    		if ( number1 > number2 && number2 > number3 )
    			largest = number1;
    		if ( number2 > number1 && number1 > number3 )
    			largest = number2;
    		if ( number3 > number2 && number2 > number1 )
    			largest = number3;
     
    		if ( number1 < number2 && number2 < number3 )
    			smallest = number1;
    		if ( number2 < number1 && number1 < number3 )
    			smallest = number2;
    		if ( number3 < number2 && number2 < number1 )
    			smallest = number3;
     
    		System.out.printf ( "%s%d\n", "The sum of your three numbers is: ", sum );
    		System.out.printf ( "%s%d\n", "The product of your three numbers is: ", product );
    		System.out.printf ( "%s%d\n", "Your three numbers produced an average of: ", average );
     
    		System.out.printf ( "\n%s%d\n", "The largest number out of the three is: ", largest );
    		System.out.printf ( "%s%d\n", "The smallest number out of the three is: ", smallest );
    	} // end method main
    }

Similar Threads

  1. ArrayIndexOutOfBoundsException issue (Beginner?)
    By cziiki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2012, 07:12 AM
  2. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM
  3. Solution for error message: Bad operand for binary operator '-'
    By MostinCredible21 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 7th, 2011, 05:28 PM
  4. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 03:49 PM
  5. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 09:55 AM