# Java Help: Decimal Format Class

• Comp Sci
• Ocasta
In summary, the area code is used to seed a random number generator. The number is then used to generate the next three phone numbers. The first number to call, the second number to call, and the third number to call are output depending on the area code input.
Ocasta

## Homework Statement

Problem:

Tele-Annoy, a telemarketing company has asked you to write a program that when given the area code to dial, generates the next three potential telephone numbers, and then outputs them to the screen for one of their sales representatives to call. Use the area code as the seed for the randomly generated numbers.

Code:
Please enter the area code: [B]860[/B]

First number to call - (860)033-8869
Second number to call - (860)367-7868
Third number to call - (860)206-1751
note: use the Random class to generate the phone numbers. use DecimalFormat class to get the leading zeroes to print out.

hint: The order you generate random numbers matters! If you use the input above, you should get the same numbers!

## The Attempt at a Solution

Here's what I have so far:

Code:
 import java.util.Scanner ;
import java.util.Random ;
import java.text.DecimalFormat ;

public class PhoneNumberGenerator
{
public static void main ( String[] args )
{
int area ;
int number ;

Scanner s = new Scanner( System.in ) ;

DecimalFormat df = new DecimalFormat( "000-0000" );

System.out.println("Please enter the area code: ");
// This gets the area code input from the user, but right now it's being occupied by 860 for debugging.
//area = s.nextInt();
area = 860 ;

Random r = new Random( area ) ;

// Test to makes sure I get the right output. Delete!
System.out.println(area) ;

number = r.nextInt( 10000000 ) ;
System.out.println("First number to call - (" + area + ")" + df.format( number ) );

number = r.nextInt( 10000000 ) ;
System.out.println("Second number to call - (" + area + ")" + df.format( number ) );

number = r.nextInt( 10000000 ) ;
System.out.println("Third number to call - (" + area + ")" + df.format( number ) );

}
}

As it stands I keep getting this:

Code:
Please enter the area code:
860 (note, this is a test to make sure I'm using the correct area code and seed)
First number to call - (860)8951033-
Second number to call - (860)5958869-
Third number to call - (860)3561367-

Last edited:
No guarantees, but give this a try:
Code:
DecimalFormat df = new DecimalFormat( "000'-0000" );

The difference is the single quote just in front of the - character. The - character is a special character, so since you want it to be part of your pattern, I believe you need to escape it, which you do by prepending a ' character.

I tried that, and it threw an exception.

Code:
PhoneNumberGenerator.java:15: error: illegal escape character
DecimalFormat df = new DecimalFormat( "000 \- 0000" )

I ended up separating the number thusly:
Code:
number = r.nextInt( 10000000 ) ;
part1 = number / 10000 ; // this gets the first three numbers
part2 = number % 10000 ; // this gets the last four numbers

System.out.println("First number to call - (" + area + ")" + part1 + "-" + part2 );

That seems to work, but I still don't understand what happened. According to the TA (and the Java documentation) it should work.

My current issue is that I have evidently seeded my random number generator incorrectly. It doesn't give me the numbers I should have. I'll let you all know how it turns out.

Ocasta said:
I tried that, and it threw an exception.

Code:
PhoneNumberGenerator.java:15: error: illegal escape character
DecimalFormat df = new DecimalFormat( "000 \- 0000" )
It looks like you used a backslash (\), not the single quote (') that I suggested. Did you try it with a single quote?

Also, I wouldn't add any spaces, as what you have above seems to show.

Yes I tried it:

Code:
Exception in thread "main" java.lang.IllegalArgumentException: Malformed pattern "000'-0000"
at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2610)
at java.text.DecimalFormat.<init>(DecimalFormat.java:435)
at PhoneNumberGenerator.main(PhoneNumberGenerator.java:15)

I used the backslash because the single quote yielded this. Also, I tried removing the spaces, but it gave me the same error.

All the TA's tell me that DecimalFormat is very finicky and that we end up throwing it away in higher level classes anyway. The professor of our class seems to be trying to walk us into classes slowly before he pushes us into the deep end.

## 1. What is the Decimal Format class in Java?

The Decimal Format class in Java is a class that allows you to format decimal numbers according to a specific pattern. It is part of the java.text package and is commonly used for formatting currency, percentages, and other numeric values.

## 2. How do I create an instance of the Decimal Format class?

To create an instance of the Decimal Format class, you will need to use the "new" keyword followed by the class name. For example, Decimal Format myFormat = new Decimal Format();

## 3. What are the different methods available in the Decimal Format class?

The Decimal Format class has several methods that allow you to format and manipulate decimal numbers. Some of the most commonly used methods include format(), parse(), setMaximumFractionDigits(), and setMinimumFractionDigits().

## 4. How can I customize the formatting pattern in the Decimal Format class?

To customize the formatting pattern, you can use the constructor that takes in a string pattern as a parameter. This pattern can include symbols such as "#" for a digit, "." for a decimal separator, and "," for a grouping separator. You can also use the setDecimalFormatSymbols() method to specify the symbols to be used.

## 5. Can the Decimal Format class handle different locales and languages?

Yes, the Decimal Format class supports locale-specific formatting. You can specify the desired locale using the constructor or use the setLocale() method to change it. This allows for localized formatting based on language and cultural conventions.

Replies
37
Views
4K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
4
Views
2K
Replies
2
Views
1K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
9K
Replies
1
Views
1K