Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Crazy Number Format

    The programming challenge in my book wants me to declare a field for an Employee number.

    The Employee number is supposed to be in the format XXX-L, where each X is a digit within the range 0-9, and L is a letter within the range A-M.

    I have no idea where to start. Is it even possible to declare all of that in just one field?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Crazy Number Format

    Are you talking about a string? Clearly you can create a string which can hold that. Or am I missing something?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Crazy Number Format

    Quote Originally Posted by jim829 View Post
    Are you talking about a string? Clearly you can create a string which can hold that. Or am I missing something?
    Well, using a string field for it would be the easy way out, but I'm supposed to update this same programming challenge at the end of the next chapter by adding exceptions. In the directions (of that next chapter), one of those exceptions is described:

    "The Employee class should throw an exception named InvalidEmployeeNumber when it recieves an employee number that is less than 0 or greater than 9999."

    I doubt a string field could be debugged that way. Here's the code of the class it's supposed to be implemented into:

    Java Code:
    package employment;
    //import java.util.Calendar;
    import org.joda.time.DateTime;
    
    
    public class Employee {
        
        public String Name;
        public String hireDate;
        public int employeeNum;//to be changed to the format XXX-L, where each X 
        //is a digit within the range 0-9, and L is a letter within the range A-M.
    
        //The following constructor accepts arguments for the employee's name,
        //hire date, and id number: 
        public Employee(String name, String aHireDate, int empNum)
            {
                Name = name;
                hireDate = aHireDate;
                employeeNum = empNum;
            }
            
            //Write one or more constructors and the appropriate accessor and 
            //mutator methods for the class. (Note that "accessor methods" are 
            //getters, and "mutator methods" are setters):
    
       
        //The setName method accepts an argument
        //that is stored in the Name field. 
        
       public void setName(String name)//error: "illegal start of expression"
       {
          Name = name;
       }
       
       
        //The setHireDate method accepts an argument
        //that is stored in the Name field.     
    
       public void setHireDate(String aHireDate)
       {
          hireDate = aHireDate;
       }
    
       
        //The setEmployeeNum method accepts an argument
        //that is stored in the payRate field.    
    
       public void setEmployeeNum(int empNum)
       {
          employeeNum = empNum;
       }
       
       
       //The getName method returns the value
       //stored in the Name field.   
    
       public String getName()
       {
          return Name;
       }   
    
       
        //The getPayRate method returns the value
        //stored in the payRate field.    
    
       public String getHireDate()
       {
          return hireDate;
       }
       
       
        //The getPayRate method returns the value
        //stored in the payRate field.    
       
       public int getEmployeeNum()
       {
          return employeeNum;
       }  
    
    }//end of Employee class (the super class)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Crazy Number Format

    Well, the only type I know that can satisfy that is a string. You can use String.format (which you probably already know) to create it. You can also parse it and throw your own exception if it doesn't conform. Also I am a little confused about your immediate requirement of XXX-L which becomes XXXX later on.

    Regards,
    Jim
    Last edited by jim829; 09-24-2013 at 11:57 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Crazy Number Format

    Actually, there may be another way. A simple class.

    Java Code:
    class ID {
      char c;
      int val;
      
      public ID (char c, int val) {
        // assignments and validity check
      }  
      public char getChar() {
          return c;
      }
      public int getNum() {
         return val;  
      }
      public void toString() {
         // override this to return the XXX-L format
      }
    }
    You could even make it immutable if you want by writing a static createInstance method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    my128samuel is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Crazy Number Format

    Hello,

    Like Jim sayed employee num should be a String format but I think you have to validate your format in the Class constructor (ex: match it with a regex), also in the setter emloyeeNum attribute and do not forget to throw exception if it is not validated with your pattern or not matched.

    Regards,

  7. #7
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Crazy Number Format

    Thanks. I'll start reading how "immutable" and "regular expressions" work, but I thought I'd ask, would it be necessary to iterate through each of the 3 XXX's one at a time, or would it be smarter to use an exception statement for all 3 of them combined?:

    if XXX is < 000 || > 999, give error message and ask user to re-enter

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Crazy Number Format

    [confused]
    First post says you need XXX-L (three digits)
    Second post says range upto 9999 (four digits)
    Which is it?

    If you have a String "123-J" you could substring the digits out, parse into an int and check if the result is in/out of range. No need to check all digits individually.

Similar Threads

  1. Number format Exception
    By know_how in forum Web Frameworks
    Replies: 2
    Last Post: 01-29-2013, 05:58 PM
  2. Number format Exception
    By aortell24 in forum New To Java
    Replies: 5
    Last Post: 07-16-2012, 06:22 AM
  3. Number format
    By asai in forum New To Java
    Replies: 12
    Last Post: 06-21-2012, 11:24 AM
  4. Number Format Exception while parsing long
    By Aamir in forum Networking
    Replies: 2
    Last Post: 05-15-2011, 03:57 AM
  5. Replies: 7
    Last Post: 12-30-2010, 02:42 PM

Posting Permissions

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