简体   繁体   中英

How to properly use a boolean in a method and print result

I am working on a program and I am almost finished with it. The only thing I can't figure out is how to as the user for ay or n and then display the boolean in the program to be true or false. It keeps giving me an error. Here is the code that I am working with. I just want it to display true or false when the last print statement in the driver executes. Thank you

public class Customer extends Person {

protected int customerNum;
protected boolean mailingList;
protected char answ;

public Customer() {
    super();
    // TODO Auto-generated constructor stub
}

public Customer(String name, String address, double telephone,
        int customerNum, boolean mailingList) {
    super(name, address, telephone);
    this.customerNum = customerNum;
    this.mailingList = mailingList;
}

/**
 * @return the customerNum
 */
public int getCustomerNum() {
    return customerNum;
}

/**
 * @param customerNum the customerNum to set
 */
public void setCustomerNum(int customerNum) {
    this.customerNum = customerNum;
}

/**
 * @return the mailingList
 */
public boolean isMailingList() {
    return mailingList;
}

/**
 * @param mailingList the mailingList to set
 */
public void setMailingList(boolean mailingList) {
    try {
        if(answ == 'y'){
            mailingList = true;
        }
        else if(answ == 'n')
            mailingList = false;
        this.mailingList = mailingList;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "Customer [customerNum=" + customerNum + ", mailingList="
            + mailingList + "]";
}

And then the driver

import java.util.Scanner;


public class CustomerDriver extends PreferredCustomer {

/**
 * @param args
 */
public static void main(String[] args) {



    Scanner kb = new Scanner(System.in);

    PreferredCustomer customer1 = new PreferredCustomer();

    System.out.println("Enter Customer Name: ");
    customer1.setName(kb.nextLine());

    System.out.println("Enter Customer Address: ");
    customer1.setAddress(kb.nextLine());

    System.out.println("Enter Telephone Number: ");
    customer1.setTelephone(kb.nextDouble());

    System.out.println("Enter the Customer Number: ");
    customer1.setCustomerNum(kb.nextInt());

    System.out.println("Does customer wish to be on mailing list? \n" + 
    "Enter 'y' for yes and 'n' for no: ");
    customer1.setMailingList(kb.nextBoolean());

    System.out.println("Enter amount of Customer's Purchase: ");
    customer1.setPurchases(kb.nextDouble());

    System.out.println("Customer's Discount is as follows: " + customer1.getDiscountLevel() + "\n");

    System.out.println("Customers Name: " + customer1.getName() + "\nCustomers Address: " + customer1.getAddress() + "\nCustomers Phone" +  customer1.getTelephone() + 
            "\nCustomer Number: " + customer1.getCustomerNum() + "\nMailing List Preferrence: " + customer1.isMailingList() + "\nCustomer's Purchase Amount " + 
            customer1.getPurchases() + "\nCustomers Discount Rate (if any) :" + customer1.getDiscountLevel());




    // TODO Auto-generated method stub

}

}

You can only use nextBoolean() when the token is literally "true" or "false" . Otherwise, it'll throw an InputMismatchException. The answers "y" and "n" don't count. You either need to prompt for the literal values "true" or "false" or else read the y/n response as a String and convert to a boolean yourself, like:

String yOrN = kb.next();
if ("y".equals(yOrN)) customer1.setMailingList(true);

Update: I see now that you already have the "if answer == 'y'" logic in your setMailingList() method. However, you're trying to pass a boolean to that method and then change the value of that boolean based on the value of the answ field, which is never assigned a value. You need to work on that part of the code before it'll work, too. I'd say you're correct to be passing a boolean to that method, so the "y or n" logic should move out of it to your main method.

y and n are not valid Booleans . You would need to input true or false for it to work.

Your best bet would be to do something like this:

if (kb.nextLine().equals("y")) {
    customer1.setMailingList(true);
} else {
    customer1.setMailingList(false);
}

you are asking user to enter char input 'y' or 'n', but your code is actually expecting 'true' or 'false'

customer1.setMailingList(kb.nextBoolean());

you may want to change it to take string input from user and check

System.out.println("Does customer wish to be on mailing list? \n" + 
    "Enter 'y' for yes and 'n' for no: ");
    customer1.setMailingList(kb.nextBoolean());

Here you're asking user to enter a character y or n . When you're calling nextBoolean , you're requesting a boolean input. Neither y or n is a boolean. A boolean is either true or false . Instead, prompt the user to enter true or false instead of y or n . Or explicitly make y = true and n = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM