Results 1 to 13 of 13
  1. #1
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default setting a boolean field when being read from a file

    i need help when i am reading in a file one of the tokens is either "yes" or "no" , i need to set the boolean field to true if it is yes.

    and false if it is no. how do i do it ?

    this is my code > public void readData(Scanner scanner)
    {
    group = scanner.next();
    vehID = scanner.next();
    regNo = scanner.next();
    make = scanner.next();
    model = scanner.next();
    if(scanner.next().equals("yes"))
    {
    airCon = true; // where i have to change the boolean to true if it is yes
    }
    engineSize = scanner.nextDouble();
    fuelType = scanner.next();
    gearbox = scanner.next();
    transmission = scanner.next();
    mileage = scanner.nextInt();
    dateFirstRegistered = scanner.next();
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: setting a boolean field when being read from a file

    Why not do this instead?

    Java Code:
    airCon= scanner.next().equals("yes");
    kind regards,

    Jos
    Last edited by JosAH; 03-09-2013 at 12:00 PM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: setting a boolean field when being read from a file

    changed it , but they are still all false in the array list when im storing it nothing is being changed

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: setting a boolean field when being read from a file

    Quote Originally Posted by ajw1993 View Post
    changed it , but they are still all false in the array list when im storing it nothing is being changed
    Aren't you curious what was actuually read? You need to be a bit creative: assign what was read to a String variable and System.out.print() it before converting it to a boolean. Seeing what it's actually doing is understandig ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: setting a boolean field when being read from a file

    its reading the yes and no which is what i want , but how do i store that in the boolean field ??

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: setting a boolean field when being read from a file

    Quote Originally Posted by ajw1993 View Post
    its reading the yes and no which is what i want , but how do i store that in the boolean field ??
    Read my original reply again: airCon= yourStringVariable.equals("yes");

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: setting a boolean field when being read from a file

    changed it and it keeps saying false . is there anything in the constuctor that i should change

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: setting a boolean field when being read from a file

    Quote Originally Posted by ajw1993 View Post
    changed it and it keeps saying false . is there anything in the constuctor that i should change
    How should we know? All you showed us is a small code snippet in your original post and it didn't contain any constructors ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: setting a boolean field when being read from a file

    heres the code (all)
    Java Code:
    import java.util.*;
    public class Vehicle
    {
        // instance variables - replace the example below with your own
        private String group,vehID, regNo, make,model,fuelType,gearbox,transmission,dateFirstRegistered;
        private boolean airCon;
        private double engineSize;
        private int mileage;
       
    
        
        /**
         * Constructor for objects of class Vehicle
         */
        public Vehicle()
        {
            // initialise instance variables
             group = null;
             vehID = null;
             regNo = null;
             make = null;
      
             
           
             
        }
        
        public String getGroup()
        {
            return group;
        }
        
        public String getVehicleID()
        {
           return vehID;
       
        }
        
        public String getRegestrationNumber()
        {
            return regNo;
        }
        
        public String getMakeOfVehicle()
        {
            return make;
    
        }
        
        public void displayDetails()
        {
            System.out.println(make + model  + " Group: " + group + " Vehicle ID: " + vehID);
            System.out.println(" Air Conditioning or Climate Control : " + airCon);
            System.out.println(" Engine Size : " + engineSize  + " Fuel : " + fuelType);
            System.out.println(" Gearbox : " + gearbox + " Transmission : " + transmission);
            System.out.println(" Mileage : " + mileage + " Date First Registered : " + dateFirstRegistered);
            
        
        }
        
        public void readData(Scanner scanner)
        {
            group = scanner.next();
            vehID = scanner.next();
            regNo = scanner.next();
            make = scanner.next();
            model = scanner.next();
            String yes = scanner.next();
             System.out.println(yes);
            engineSize = scanner.nextDouble();
            fuelType = scanner.next();
            gearbox = scanner.next();
            transmission = scanner.next();
            mileage = scanner.nextInt();
            dateFirstRegistered = scanner.next();
        }
    }

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: setting a boolean field when being read from a file

    In your readData( ... ) method you're not setting your airCon member variable anywhere ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: setting a boolean field when being read from a file

    what like airCon = scanner.next(); or next Boolean

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: setting a boolean field when being read from a file

    Quote Originally Posted by ajw1993 View Post
    what like airCon = scanner.next(); or next Boolean
    Don't guess; if you'd read the API documentation for the Scanner class you could've read that a String representation of boolean value is "true" or "false"; that's no what you want. Also, read my original reply again; it answers your question (but as usual it is ignored).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: setting a boolean field when being read from a file

    thanks ive got it to work , but now i need help on writing a class for testing can you help me ?

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2012, 11:53 AM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. Boolean methods & how to read this in plain English
    By bigsonny in forum New To Java
    Replies: 6
    Last Post: 06-19-2011, 09:27 PM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. Replies: 1
    Last Post: 10-10-2008, 08:47 AM

Posting Permissions

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