Java Help: Decimal Format Class

  • Context: Comp Sci 
  • Thread starter Thread starter Ocasta
  • Start date Start date
  • Tags Tags
    Class Format Java
Click For Summary

Discussion Overview

The discussion revolves around a Java programming assignment that involves generating random phone numbers based on a given area code. Participants are addressing issues related to the use of the DecimalFormat class for formatting the output of the generated numbers.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their initial code for generating phone numbers and notes that the output is not formatted correctly, specifically mentioning the use of the DecimalFormat class.
  • Another participant suggests using a single quote to escape the hyphen in the DecimalFormat pattern, proposing the format "000'-0000".
  • A participant reports an exception when trying the suggested format, indicating an illegal escape character and providing an alternative method of separating the generated number into parts for formatting.
  • Further clarification is provided by another participant, who points out that the original suggestion used a backslash instead of a single quote, which may have caused the error.
  • Another participant confirms that they encountered the same exception with the single quote format and mentions that DecimalFormat can be finicky, referencing advice from teaching assistants about its use in higher-level classes.

Areas of Agreement / Disagreement

Participants express differing experiences with the DecimalFormat class and its patterns, indicating a lack of consensus on the correct approach to formatting the phone numbers. The discussion remains unresolved regarding the best method to achieve the desired output.

Contextual Notes

Participants note issues related to the specific formatting requirements of the DecimalFormat class, including the handling of special characters and the potential for exceptions when using certain patterns.

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 37 ·
2
Replies
37
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
2K