views:

200

answers:

6

Hello, I am trying to make my boolean value work. I am new at programming java so I must be missing something simple. I am trying to make it so that if one of the tire pressures is below 35 or over 45 the system outputs "bad inflation" For class I must use a boolean which is what I tried. I cant figure out why this isnt working. No matter what I do the boolean is always true. Any tips?

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
+14  A: 
    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.

polygenelubricants
You also need to initialize goodPressure to true, otherwise it will always be false.
Nate Heinrich
Thank you sir! I knew it was something simple !
Cheesegraterr
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.
Seth
+1 for "Or even better yet: if (goodPressure)"
Michael Shimmins
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.
Elliot
It's funny how this ancient mistake keeps reappearing over and over... :S +1 to the answer from me.
dimitko
+1  A: 

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

Nate Heinrich
+1 Nice catch!!
polygenelubricants
A: 

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)
Andy White
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()

CaptainAwesomePants
A: 

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?

hdk
A: 

Usually, code like if (variable = constantValue) is treated as compilation error in Java. However, there is an exception when the constant value is a boolean. In that case, the statement is equal to if (constantValue). This kind of issue can not be found in the compilation phase.

So, I suggest 1)do not compare with boolean constant value, just do it by if (booleanVar); 2)always put the constant value ahead, like 'if (true = variable)' will cause the compilation fail.

Chris