Why does my boolean test in java always fail?

Related searches

I am trying to make a boolean test so that if one of the tire pressures is below 35 or over 45 the system outputs "bad inflation".

In my class I must use a boolean, which is what I tried. However the boolean returned is always true. I don't understand why.

public class tirePressure
{
    private static double getDoubleSystem1 ()  //Private routine to simply read a double in from the command line
    {
        String myInput1 = null; //Store the string that is read form the command line
        double numInput1 = 0;      //Used to store the converted string into an double
        BufferedReader mySystem; //Buffer to store input
        mySystem = new BufferedReader (new InputStreamReader (System.in)); // creates a connection to system files or cmd
        try
        {
            myInput1 = mySystem.readLine (); //reads in data from console
            myInput1 = myInput1.trim (); //trim command cuts off unneccesary inputs
        }
        catch (IOException e)  //checks for errors
        {
            System.out.println ("IOException: " + e);
            return -1;
        }

        numInput1 = Double.parseDouble (myInput1); //converts the string to an double
        return numInput1;                       //return double value to main program
    }

    static public void main (String[] args)
    {
        double TireFR; //double to store input from console
        double TireFL;
        double TireBR;
        double TireBL;
        boolean goodPressure;
        goodPressure = false;

        System.out.println ("Tire Pressure Checker");
        System.out.println (" ");

        System.out.print ("Enter pressure of front left tire:");
        TireFL = getDoubleSystem1 ();    //read in an double from the user

        if (TireFL < 35 || TireFL > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;
        }

        System.out.print ("Enter pressure of front right tire:");
        TireFR = getDoubleSystem1 ();    //read in an double from the user

        if (TireFR < 35 || TireFR > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;

        }

        if (TireFL == TireFR)
            System.out.print (" ");
        else
            System.out.println ("Front tire pressures do not match");
        System.out.println (" ");

        System.out.print ("Enter pressure of back left tire:");
        TireBL = getDoubleSystem1 ();    //read in an double from the user

        if (TireBL < 35 || TireBL > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;
        }

        System.out.print ("Enter pressure of back right tire:");
        TireBR = getDoubleSystem1 ();    //read in an double from the user

        if (TireBR < 35 || TireBR > 45)
        {
            System.out.println ("Pressure out of range");
            goodPressure = false;
        }

        if (TireBL == TireBR)
            System.out.print (" ");
        else
            System.out.println ("Back tire pressures do not match");

        if (goodPressure = true)
            System.out.println ("Inflation is OK.");
        else
            System.out.println ("Inflation is BAD.");

        System.out.println (goodPressure);


    } //mainmethod
} // tirePressure Class
    if (goodPressure = true)

Change this to:

    if (goodPressure == true)

Or even better yet:

    if (goodPressure)

Boolean comparison operators are == and !=. The = is an assignment operator.

Also, you need to initially set goodPressure = true; before you check for violating conditions.

Conditionals and logic | Think Java, The caller can check for a return value just as easily as catch an exception then the bottleneck is always going to be the network and file system I/O, But a method failing to fulfill its purpose is an exceptional case (or at least But we have checked exceptions in Java, so I see no reason not to use them. Boolean Expression. A Boolean expression is a Java expression that returns a Boolean value: true or false. You can use a comparison operator, such as the greater than (>) operator to find out if an expression (or a variable) is true:

you are initializing goodPressure to false, but then never assigning true, so it will always be false. Try initializing it to true.

Function returning true/false vs. void when succeeding and throwing , In your case you was trying to compare Boolean value with String value method has returned the Boolean value that's why your test case fail. Often we assign a boolean to true or false as we declare it. Here we test for truth in two ifs. True: The program first assigns the boolean of name "value" to true. The if-statement then detects "value" is true. False: We then set the same boolean variable to false. Now the !value test evaluates to true, so the "B" is printed.

It looks like you're never setting goodPressure to true. Maybe you want to start with it set to true, as it looks like your conditions will set it to false if necessary.

Also, I think this line should throw a compiler warning (or error?)

if (goodPressure = true)

when compiling in Java. I thought the compiler wouldn't let you do an assignment in an if check, but maybe it does... I think you want it to be:

if (goodPressure == true)

Or just:

if (goodPressure)

assertequals() returns true but make my test case failed, bool b1 { !true }; // b1 will be initialized with the value false In C++11 or newer, a failed input will also zero-out the variable, so b also gets assigned value 0. Boolean values are often used as the return values for functions that check When the user inputs "false" or "true", the program always prints "Boolean value: 0",� Java provides a wrapper class Boolean in java.lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a

Your problem is that there is only a single = sign in the expression if (goodPressure = true). That assigns true to goodPressure and then checks to see if goodPressure is still true.

You need to use a == or a .equals()

4.9 — Boolean values, Now the !value test evaluates to true, so the "B" is printed. Java program that uses boolean, true, false public class Program { public static void main(String[] args)� In some languages, such as C or C++, integer values can be treated as a boolean, with 0 equal to false and any other value equal to true. Not so in Java. In Java, you can’t convert between an integer type and a boolean type. A Boolean expression is a Java expression that, when evaluated, returns a Boolean value: true or false.

Look at the last if statement. You are doing assignment not comparison.

BTW. Your program will always return false once you do that... look at your logic. Where do you set goodPressure to true?

Java Boolean Examples, What is Java Update and how do I change the update schedule? User Account Control (UAC) in Windows; Why do I see the Java Update Needed message: Java is out of date? Java update settings are not saved in the Java control panel

The above code will work fine. It has a boolean test checking for low fuels, and depending on that it runs either "return true;" or "return false;". But there is a better way. We can cut out the if-statement middle-man. Notice that the boolean in the if-test (true or false) happens to be the same as the value we want to return.

Java has. int and Integer; boolean and Boolean; This seems a bit inconsistent, why not either. bool vs Boolean to use an established shorter name for primitive type? or. integer vs Integer to keep type names consistent? I think C++ had decided to use bool quite a bit earlier than Java decided to use boolean, and maybe

» Download and Install Java » Use Java » Test Java » Remove Old Versions of Java » Find Java » Always redirected to the java.com download page Chrome browser versions 42 and above. Starting with Chrome version 42 (released April 2015), Chrome has disabled the standard way in which browsers support plugins.

Comments
  • your code is clear enough that you don't need the comments so much as they just repeat the same information (that's a good thing!). for future reference, the most important point to remember is to explain WHY you are doing something, not WHAT you are doing. :D
  • You may also want to take a look at java.util.Scanner.
  • You also need to initialize goodPressure to true, otherwise it will always be false.
  • Thank you sir! I knew it was something simple !
  • If you find yourself writing this kind of bug a lot, you can make the compiler catch it by putting constants on the left side of the == (instead of variables). For example, if (true = goodPressure) generates a compiler error.
  • Definitely agree with shimms above. In times when you're checking for null or comparing to a value, consider putting the constant on the left-side. This way, if you use = instead of ==, you'll get a compile-time error.
  • It's funny how this ancient mistake keeps reappearing over and over... :S +1 to the answer from me.