Results 1 to 7 of 7
  1. #1
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default Read a file bit by bit?

    Hello. I have tried to google for this information, but people either instantly refer to the readByte() method in the various I/O packages or just say that it can't be done.

    Anyways, I have ONE byte, in which each one of the 8 bits means a training for an employee in my data..

    for instance...

    The left most bit indicates orientation training
    The 2nd bit from left indicates management training
    The 3rd bit from left indicates technical training
    The 4th bit from left indicates operations training
    The 5th bit from left indicates administrative training
    The 6th bit from left indicates quality control training
    The 7th bit from left indicates sales training
    The 8th bit from left indicates safety training

    The problem is, I cannot figure out how to read in what trainings an employee has. If I had a readBit() method, I could easily use If Statements, etc. If the bit is switched on (1), then it means they have the training. This is the one stump I have left to overcome.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why can't you read in the byte and then operating on the byte to get your information?
    I am unfamiliar on operating with the byte. As in ask if the byte is a certain value? Because, I think I wouldn't be able to, since my data allows a employee to have more then 1 type of training, i would have to state every possibility...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zerkz View Post
    I am unfamiliar on operating with the byte. As in ask if the byte is a certain value? Because, I think I wouldn't be able to, since my data allows a employee to have more then 1 type of training, i would have to state every possibility...
    You owe it to yourself to familiarize yourself with byte manipulation. You could have several byte constants as your masks and do a simple bitwise AND operations (using the & operator) to easily extract the different types of training an employee has performed. If this doesn't make sense, you may wish to google Java bitwise operations tutorials and see what comes up.

    Much luck

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no expert in this (so corrections MOST welcome), but an enum could work nicely:
    Java Code:
    public enum TrainingMasks {
      ORIENTATION(0x80),
      MANAGEMENT(0x40),
      TECHNICAL(0x20),
      OPERATIONS(0x10),
      ADMINISTRATION(0x08),
      QUALITY(0x04),
      SALES(0x02),
      SAFETY(0x01);
      
      private int mask;
      private TrainingMasks(int mask) {
        this.mask = mask;
      }
      
      public int getMask() {
        return mask;
      }
      
      public boolean isTrained(int test) {
        return (test & mask) != 0;
      }
    }

    Java Code:
    public class TestMasks {
      public static void main(String[] args) {
        byte test = (byte)0xf2;
        for (TrainingMasks mask : TrainingMasks.values()) {
          if (mask.isTrained(test)) {
            System.out.println("Mask: " + mask + " is OK");
          }
        }
      }
    }
    Last edited by Fubarable; 12-12-2009 at 09:39 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Or you could use an EnumSet:

    Java Code:
    import java.util.Collections;
    import java.util.EnumSet;
    import java.util.Set;
    
    public class Training {
        public enum TrainingType {
            ORIENTATION, MANAGEMENT, TECHNICAL, OPERATIONS,
            ADMINISTRATION, QUALITY, SALES, SAFETY
        }
    	
        private EnumSet<TrainingType> types = EnumSet.noneOf(TrainingType.class);
    	
        public Training(byte b) {
            for(TrainingType t :TrainingType.values()) {
                if((b & 1 << t.ordinal()) != 0) {
                    types.add(t);
                }
            }
        }
    	
        public Set<TrainingType> getCanDo() {
            return Collections.unmodifiableSet(types);
        }
    	
        public Set<TrainingType> getCantDo() {
            return EnumSet.complementOf(types);
        }
    	
        public void add(TrainingType newT) {
            types.add(newT);
        }
    	
        public boolean isTrained(TrainingType testT) {
            return types.contains(testT);
        }
    	
        public static void main(String[] args) {
            Training test = new Training((byte)0xf2);
            System.out.printf("Can do: %s%n", test.getCanDo());
            System.out.printf("Can't do: %s%n%n", test.getCantDo());
    		
            test.add(TrainingType.TECHNICAL);
            for(TrainingType type :TrainingType.values()) {
                System.out.printf("Can %s: %s%n", type, test.isTrained(type));
            }
        }
    }
    Last edited by pbrockway2; 12-12-2009 at 11:24 PM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry: put the TrainingType enum in its own file.

    Also you can test whether a given level of training qualifies someone for a given task - since both tasks and people might have training associated with them:

    Java Code:
    // in Training.java
    
    public boolean isTrained(Training testTask) {
        for(TrainingType t :testTask.types) {
            if(!types.contains(t)) {
                return false;
            }
        }
        return true;
    }
    
    // work like this:
    
    Training task = new Training((byte)0);
    task.add(TrainingType.TECHNICAL);
    task.add(TrainingType.OPERATIONS);
    System.out.printf("%n%s can do %s: %s%n", test, task.getCanDo(), test.isTrained(task));

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  2. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  3. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  4. file read
    By makpandian in forum New To Java
    Replies: 3
    Last Post: 12-11-2008, 04:02 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •