Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: setting a boolean in a field when being read from a file

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default setting a boolean in a field when being read from a file

    Hello i need help i have to read in a file , but one of the tokens in the file is either yes or no depending on if the vehicle has air con or not.
    how do i change the boolean field to true if it is yes and no to false ???.

    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 it has to be yes or no
    }
    engineSize = scanner.nextDouble();
    fuelType = scanner.next();
    gearbox = scanner.next();
    transmission = scanner.next();
    mileage = scanner.nextInt();
    dateFirstRegistered = scanner.next();
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What happens when you compile and execute the code?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    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();
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What happens when you compile and execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    it reads in the file perfect
    , but the boolean field always says false when im trying to store that data which has to be either yes or no depending on what it says on the line in the file

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    the boolean field always says false
    Read the String into a variable and print out the value of that variable to be sure that it contains the String that you think it should.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    it does it reads both yes n no in the order of the lines which is good , but i need to store them in a boolean

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Are you using the code shown in post#1 to set the boolean?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    yeah i only changed it to see if it was reading the yes n no's.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Did it work as you wanted?

    Please post the current version of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    no it kept saying false when trying to store them
    >
    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);
             airCon = yes.equals("yes");
            engineSize = scanner.nextDouble();
            fuelType = scanner.next();
            gearbox = scanner.next();
            transmission = scanner.next();
            mileage = scanner.nextInt();
            dateFirstRegistered = scanner.next();
        }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Does that code work? What is printed out?
    Change the println to:
           System.out.println("yes="+yes +"<");
    to see what was read and tested.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    it prints out the yes n no in each line, so it means that it reads what i want to storee, but not store it

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Can you copy and paste here what is printed out?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    yes=No<
    yes=No<
    yes=No<
    yes=Yes<
    yes=Yes<
    yes=Yes<
    yes=Yes<
    yes=Yes<
    yes=No<
    yes=No<
    yes=Yes<
    yes=No<
    yes=No<
    yes=No<
    yes=No<
    yes=No<
    yes=No<
    yes=No<
    yes=Yes<
    yes=Yes<
    yes=Yes<

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    The code:
     airCon = yes.equals("yes");
    The Strings that were read:
    yes=Yes<
    yes=Yes<

    Notice the difference of case in the spelling: Y is not equal to y
    The String class has a method for comparing Strings that ignores case. Try that.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    ajw1993 (March 10th, 2013)

  18. #17
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    thank you man it works . can you do something else for me im just confused on how to write a test class on testing how to create 3 objects and adding them to an array list.

Similar Threads

  1. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  2. [SOLVED] Setting field width System.out.printf
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 18th, 2012, 01:55 PM
  3. Setting Boolean to 'True' after X amount of Seconds?
    By uhKenKaniff in forum Java Theory & Questions
    Replies: 1
    Last Post: December 12th, 2011, 07:29 PM
  4. Setting constraints on a field
    By cslx99 in forum Object Oriented Programming
    Replies: 11
    Last Post: December 8th, 2011, 03:49 AM
  5. Do I have to keep setting the path file for javac?
    By fallout87 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 24th, 2011, 07:33 PM