Ty Givens
Ty Givens

Reputation: 111

Boolean value won't change

I'm working on a small program that basically just shows that I know how to use superclasses. Everything works great except my boolean value. It's supposed to ask the use of they would like to sign up for the mailing list. If "yes" is entered, the boolean should be true, if "no" is entered it should be false. My problem is that even when I enter "no" it still registers as "yes" making the boolean value true. Please help? (I hope that made some semblance of sense.)

 import java.util.Scanner;

public class CustomerDemo 
{
    public static void main(String[] args) 
    {
        String name;
        String address;
        String telephone;
        int customerNumber;
        String input;
        boolean mail = false;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter your name: ");
        name = keyboard.nextLine();

        System.out.print("Enter your address: ");
        address = keyboard.nextLine();

        System.out.print("Enter your phone number: ");
        telephone = keyboard.nextLine(); 

        System.out.print("Enter your customer number: ");
        customerNumber = keyboard.nextInt();

        keyboard.nextLine();

        System.out.print("Do you want to be on the mailing list (Yes or No): ");
        input = keyboard.nextLine();

        if (input.equalsIgnoreCase("yes")) {
            mail = true;
        }

        Customer cust = new Customer(name, address, telephone, customerNumber, mail);

        System.out.println("Hello " + cust.getName() + "!");

        System.out.println("You are customer " + cust.getCustomerNumber() + ".");


        if(mail = false){
            System.out.println("You are not on the mailing list. ");
        }

        else {
            System.out.println("You are on the mailing list. "); 
        }
    }
}

Upvotes: 0

Views: 2156

Answers (3)

sunshine
sunshine

Reputation: 166

look at this line if (mail = false) this should be if(!mail)

Upvotes: 2

Vineet Singla
Vineet Singla

Reputation: 1677

There is nothing wrong in your reading code.. Error is where you are trying to print. The condition if(mail = false) is incorrect.. It should be if(mail==false)

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347334

You're not doing a comparision, you're doing an assignment...

This if(mail = false), equates to if (false) because false is assigned back to mail...

Do if(!mail) this instead, which equates to if (!true) (or you can also do if(mail == false) if you really, really want to)

Upvotes: 4

Related Questions