1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Help: Decimal Format Class

  1. Jan 31, 2013 #1
    1. The problem statement, all variables and given/known data

    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 (Text):

    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!


    3. The attempt at a solution

    Here's what I have so far:

    Code (Text):


     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 (Text):

    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: Jan 31, 2013
  2. jcsd
  3. Feb 1, 2013 #2

    Mark44

    Staff: Mentor

    No guarantees, but give this a try:
    Code (Text):
    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.
     
  4. Feb 1, 2013 #3
    I tried that, and it threw an exception.

    Code (Text):

    PhoneNumberGenerator.java:15: error: illegal escape character
            DecimalFormat df = new DecimalFormat( "000 \- 0000" )
     
    I ended up separating the number thusly:
    Code (Text):

    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.
     
  5. Feb 1, 2013 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Feb 1, 2013 #5
    Yes I tried it:

    Code (Text):

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java Help: Decimal Format Class
  1. Java class Arrays (Replies: 1)

Loading...