Comp Sci Java Help: Decimal Format Class

AI Thread Summary
The discussion focuses on a Java programming assignment to generate phone numbers based on a given area code using the Random and DecimalFormat classes. The user is experiencing issues with formatting the generated numbers correctly, particularly with leading zeros and the placement of hyphens. Suggestions include using a single quote to escape the hyphen in the DecimalFormat pattern, but the user encounters exceptions when implementing this. Additionally, there are discussions about the correct seeding of the random number generator to ensure consistent output. Overall, the conversation highlights common challenges faced when working with Java's formatting and random number generation.
Ocasta
Messages
40
Reaction score
0

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:
Physics news on Phys.org
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.
 

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
2
Views
1K
Replies
7
Views
3K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
10K
Replies
1
Views
2K
Back
Top