views:

441

answers:

3

I need to return a string in the form xxx-xxxx where xxx is a number and xxxx is another number, however when i have leading zeros they disappear. I'm trying number formatter, but it's not working.

 public String toString(){
        NumberFormat nf3 = new DecimalFormat("#000");
        NumberFormat nf4 = new DecimalFormat("#0000");
        if( areaCode != 0)
            return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
        else
            return exchangeCode + "-" + number;
    }

}

I figured it out:

 public String toString(){
        NumberFormat nf3 = new DecimalFormat("000");
        NumberFormat nf4 = new DecimalFormat("0000");
        if( areaCode != 0)
            //myFormat.format(new Integer(someValue));
            return nf3.format(new Integer(areaCode)) + "-" + nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
        else
            return nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
    }
+2  A: 

Remove the # sign

http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

This code:

import java.text.DecimalFormat;
import java.text.NumberFormat;


public class Test
{

    public static void main(String[] args) 
    {       
        int areaCode = 123;
        int exchangeCode = 456;

        NumberFormat nf3 = new DecimalFormat("0000");

        System.out.println(nf3.format(areaCode) + "-" + nf3.format(exchangeCode) );
    }

}

Produces this output:

0123-0456

Holograham
no that didn't work
That's not what he's looking for. The format is not supposed to be 2 4-digit numbers. It's telephone number format, ###-####.
Justin Ardini
meh the point wasnt to replicate his problem it was to show how to add leading zeros...this way works
Holograham
+2  A: 

When areaCode is 0, you forget to call format! Other than that, it looks fine. The leading "#" are not necessary, but won't cause any problems for valid inputs.

I just tried it out real quick to check and it worked fine for me.

public static String formatTest(int areaCode, int exchangeCode, int number) {
    DecimalFormat nf3 = new DecimalFormat("#000");
    DecimalFormat nf4 = new DecimalFormat("#0000");
    if( areaCode != 0)
        return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
    else
        return nf3.format(exchangeCode) + "-" + nf4.format(number);
}


public static void main(String[] args) {
    System.out.println(formatTest(12, 90, 8));
    System.out.println(formatTest(1, 953, 1932));
}

Output:

012-090-0008
001-953-1932
Justin Ardini
No offense but, String.format is the easiest way to deal with this.
ring bearer
None taken. String format is indeed more elegant, but the poster already had a solution using DecimalFormat and was just asking why it wasn't working.
Justin Ardini
+5  A: 

There's an arguably more elegant solution:

String.format("%04d-%03d-%03d", areaCode, exchangeCode, number)
Tomislav Nakic-Alfirevic
+1 - i was about to suggest
ring bearer
+1 much more elegant and concise. Wouldn't think of any other solution.
Yuval A