Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Exclamation 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;
            }
    If there is an areaCode, exchangeCode or number that begins with a zero, how do I replace these zeroes as they are lost when the code below is executed?
    Java Code:
     areaCode=Integer.parseInt(matcher.group(1));
    exchangeCode = Integer.parseInt(matcher.group(2));
    number = Integer.parseInt(matcher.group(3));

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Cool

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by SteroidalPsycho View Post
    If there is an areaCode, exchangeCode or number that begins with a zero, how do I replace these zeroes as they are lost when the code below is executed?
    A phone number is not a number; numbers don't have leading zeros. Keep that phone number as a String type value.

    kind regards,

    Jos

  5. #5
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by SteroidalPsycho View Post
    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.
    If your phone 'number' always has 10 digits conceptually you know what to do if your int implementation thereof < 10^10 (check the formatted print method(s).

    kind regards,

    Jos

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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

  1. Shift Off Trailing Zeroes
    By nwboy74 in forum New To Java
    Replies: 5
    Last Post: 02-25-2010, 08:56 AM
  2. Saving the index??
    By vividcooper in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 11:20 PM
  3. Saving changes done through a program
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 05:53 PM
  4. Saving Values
    By Sysem in forum New To Java
    Replies: 10
    Last Post: 06-02-2008, 07:29 PM
  5. Saving Variables
    By Fish in forum New To Java
    Replies: 6
    Last Post: 06-25-2007, 09:20 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
  •