Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly use a boolean in a method and print result
    primarykey
    data
    text
    <p>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 a y 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 </p> <pre><code>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 + "]"; } </code></pre> <p>And then the driver</p> <pre><code>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 } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload