Results 1 to 7 of 7
- 04-14-2010, 08:38 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
help with saving bleeding zeroes.
I have a constructor and an overridden toString() as follows:
Java Code:public tlf0096Prog3a(String aString){ //pattern and matcher to test for correct phone number format of xxx-xxx-xxxx. Pattern pattern = Pattern.compile("(\\d{3})-(\\d{3})-(\\d{4})"); Matcher matcher = pattern.matcher(aString); try { //if it doesn't match the correct format try xxx-xxxx pattern if(!matcher.matches()){ pattern = Pattern.compile("(\\d{3})-(\\d{4})"); matcher = pattern.matcher(aString); //if still no match then throw exception if (!matcher.matches()) throw new InvalidPhoneException(); /*else sets exchangeCode and number to xxx-xxxx formatted phone number *using regex grouping. */ else { exchangeCode = Integer.parseInt(matcher.group(1)); number = Integer.parseInt(matcher.group(2)); } } /* sets areaCode, exchangeCode and number to xxx-xxx-xxxx formatted * phone number using regex grouping. */ else { areaCode = Integer.parseInt(matcher.group(1)); exchangeCode = Integer.parseInt(matcher.group(2)); number = Integer.parseInt(matcher.group(3)); } } //catch InvalidPhoneException and inform user. catch(InvalidPhoneException e){ System.out.println(e.getMessage()); System.exit(0); } }//end constructor //toString() returns a String representation of an object @Override public String toString(){ if(areaCode!=0) return areaCode+"-"+exchangeCode+"-"+number; else return exchangeCode+"-"+number; }
Java Code:areaCode=Integer.parseInt(matcher.group(1)); exchangeCode = Integer.parseInt(matcher.group(2)); number = Integer.parseInt(matcher.group(3));
- 04-14-2010, 08:59 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Uhm, by simply saving those as Strings? They are not, really, numbers, you know? They are codes that just happen to use numbers for the symbols.
- 04-14-2010, 09:07 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
I'm in college and my professor specifically told us that we have to have the instance variables set as ints. I need a way to add zeroes to the codes that get one or more dropped because of the parsing to an int.
Thanks
tim
- 04-14-2010, 09:07 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 04-14-2010, 09:14 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
I know u would never do it this way bit my professor is incompetent and I just want to please him for an a. He says there is a way to do this.
- 04-14-2010, 09:17 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 04-14-2010, 09:55 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
As already alluded to, all you can do is "left-pad" the "number" to the designated length in toString as numbers, as already said, do not have leading zeroes. You can use the format methods of String, or even the braindead version of concatenating a String of length x 0 with your "number" and substringing the length from the right end.
Similar Threads
-
Shift Off Trailing Zeroes
By nwboy74 in forum New To JavaReplies: 5Last Post: 02-25-2010, 08:56 AM -
Saving the index??
By vividcooper in forum New To JavaReplies: 1Last Post: 01-27-2010, 11:20 PM -
Saving changes done through a program
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 05:53 PM -
Saving Values
By Sysem in forum New To JavaReplies: 10Last Post: 06-02-2008, 07:29 PM -
Saving Variables
By Fish in forum New To JavaReplies: 6Last Post: 06-25-2007, 09:20 PM
Bookmarks