Results 1 to 8 of 8
Thread: Crazy Number Format
- 09-24-2013, 11:00 PM #1
Senior Member
- Join Date
- Jul 2013
- Location
- Wisconsin, USA
- Posts
- 106
- Rep Power
- 0
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?
- 09-24-2013, 11:09 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-24-2013, 11:26 PM #3
Senior Member
- Join Date
- Jul 2013
- Location
- Wisconsin, USA
- Posts
- 106
- Rep Power
- 0
Re: Crazy Number Format
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)
- 09-24-2013, 11:33 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimLast 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
- 09-25-2013, 12:07 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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 } }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-25-2013, 12:12 AM #6
Member
- Join Date
- Sep 2013
- Posts
- 6
- Rep Power
- 0
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,
- 10-12-2013, 05:42 AM #7
Senior Member
- Join Date
- Jul 2013
- Location
- Wisconsin, USA
- Posts
- 106
- Rep Power
- 0
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
- 10-12-2013, 06:18 AM #8
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
-
Number format Exception
By know_how in forum Web FrameworksReplies: 2Last Post: 01-29-2013, 05:58 PM -
Number format Exception
By aortell24 in forum New To JavaReplies: 5Last Post: 07-16-2012, 06:22 AM -
Number format
By asai in forum New To JavaReplies: 12Last Post: 06-21-2012, 11:24 AM -
Number Format Exception while parsing long
By Aamir in forum NetworkingReplies: 2Last Post: 05-15-2011, 03:57 AM -
catch the number format exception, where to place try
By aborgeld in forum New To JavaReplies: 7Last Post: 12-30-2010, 02:42 PM
Bookmarks