Error message: operator < cannot be applied to boolean,int

19,491

You can't do thirdnumber < secondnumber < firstnumber in Java, as it interprets it as (thirdnumber < secondnumber) < firstnumber, comparing true or false to the number.

What you want is to combine two conditions like this:

if ( firstnumber < secondnumber && secondnumber < thirdnumber )
result = firstnumber + " is the smallest number!";
Share:
19,491
user820913
Author by

user820913

Updated on June 05, 2022

Comments

  • user820913
    user820913 almost 2 years

    I´m new to programming, and are doing som exercices. In this exercice I am supposed to write a program that reads in three numbers from the user of the program. The program is supposed to find the smallest number, and print which one is the smallest number. Here is my code:

    import javax.swing.JOptionPane;
    
    public class Smallestnumber
    {
      public static void main( String args[] )
        {
        // Defining variables:
        String firstnumberstring;
        String secondnumberstring;
        String thirdnumberstring;
        String result;
        int firstnumber;
        int secondnumber;
        int thirdnumber;
    
        // Making input frames:
        firstnumberstring = JOptionPane.showInputDialog( "Write first number!" );
        secondnumberstring = JOptionPane.showInputDialog( "Write second number!");
        thirdnumberstring = JOptionPane.showInputDialog( "Write third number!" );
    
        // Converting stringvalues to int values:
        firstnumber = Integer.parseInt( firstnumberstring );
        secondnumber = Integer.parseInt( secondnumberstring );
        thirdnumber = Integer.parseInt( thirdnumberstring );
    
        // Initialising printstring to an empty string:
        result = "";
    
        if ( firstnumber < secondnumber < thirdnumber )
        result = firstnumber + " is the smallest number!";
    
        if ( firstnumber < thirdnumber < secondnumber )
        result = firstnumber + " is the smallest number!";
    
        if ( secondnumber < firstnumber < thirdnumber )
        result = secondnumber + " is the smallest number!";
    
        if ( secondnumber < thirdnumber < firstnumber )
        result = secondnumber + " is the smallest number!";
    
        if ( thirdnumber < firstnumber < secondnumber )
        result = thirdnumber + " is the smallest number!";
    
        if ( thirdnumber < secondnumber < firstnumber )
        result = thirdnumber + " is the smallest number!";
    
        // Making conclusion box:
        JOptionPane.showMessageDialog( null, result, "Conclusion:", JOptionPane.INFORMATION_MESSAGE );
    
        } // End of main method
    } // End of class Smallestnumber
    

    And here is the error messages:

    Smallestnumber.java:29: operator < cannot be applied to boolean,int
        if ( firstnumber < secondnumber < thirdnumber )
                                        ^
    Smallestnumber.java:32: operator < cannot be applied to boolean,int
        if ( firstnumber < thirdnumber < secondnumber )
                                       ^
    Smallestnumber.java:35: operator < cannot be applied to boolean,int
        if ( secondnumber < firstnumber < thirdnumber )
                                        ^
    Smallestnumber.java:38: operator < cannot be applied to boolean,int
        if ( secondnumber < thirdnumber < firstnumber )
                                        ^
    Smallestnumber.java:41: operator < cannot be applied to boolean,int
        if ( thirdnumber < firstnumber < secondnumber )
                                       ^
    Smallestnumber.java:44: operator < cannot be applied to boolean,int
        if ( thirdnumber < secondnumber < firstnumber )
                                        ^
    6 errors
    

    How can I get the program to work?