Results 1 to 3 of 3

Thread: Return Boolean

  1. #1
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Return Boolean

    Java Code:
    public class InnerInsulation {
    
        public InnerInsulation() {
            Scanner in = new Scanner(System.in);
    
            CheckColour cl = new CheckColour();
    
            if (cl.getUserColour() == 0 || cl.getUserColour() == 1) {
                System.out.println("Inner insulation is not available for the number of colour(s) choosen");
            } else if (cl.getUserColour() == 2) {
                System.out.println("Would you like insulation?");
                String insulation = in.next();
    
                boolean wantsInsulation = (insulation.equalsIgnoreCase("Y") || insulation.equalsIgnoreCase("YES"));
                boolean noInsulation = (insulation.equalsIgnoreCase("N") || insulation.equalsIgnoreCase("NO"));
    
                if (wantsInsulation) {
                    System.out.println("Insulation required");
                } else if (noInsulation) {
                    System.out.println("No insulation");
                }
            }
        }
    }
    I need to return the users answer (boolean) so i can access in another class. How can i do this? I would return and then the variable but it is dependant is user selects yes or no which being 2 different variables im not sure how to go about this.

    Please advice

  2. #2
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: Return Boolean

    First of all, you only need one boolean to handle whether the user wants insulation - just set:
    Java Code:
    boolean wantsInsulation = (insulation.equalsIgnoreCase("Y") || insulation.equalsIgnoreCase("YES"));
    If it's true, the user wants insulation, and if it's false, the user doesn't want it.

    To return wantsInsulation, you can set a class variable, and write an accessor (or "getter") method. Here's how that might look:

    Java Code:
    public class InnerInsulation {
        
        boolean wantsInsulation;
     
        public InnerInsulation() {
            Scanner in = new Scanner(System.in);
     
            CheckColour cl = new CheckColour();
     
            if (cl.getUserColour() == 0 || cl.getUserColour() == 1) {
                System.out.println("Inner insulation is not available for the number of colour(s) choosen");
            } else if (cl.getUserColour() == 2) {
                System.out.println("Would you like insulation?");
                String insulation = in.next();
     
                wantsInsulation = (insulation.equalsIgnoreCase("Y") || insulation.equalsIgnoreCase("YES"));            
            }
        }
        public boolean getWantsInsulation() {
            return wantsInsulation;
        }
    }
    Another class could initiate an Insulation object, and then call getWantsInsulation() to find out what the user said.

  3. #3
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  2. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  3. help with recursive return boolean
    By Yakg in forum New To Java
    Replies: 11
    Last Post: 01-11-2011, 05:54 PM
  4. Return Statement and Boolean Help
    By GhostShaman in forum New To Java
    Replies: 8
    Last Post: 03-09-2010, 11:15 AM
  5. Boolean Return Value
    By devstarter in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 07:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •