Results 1 to 5 of 5
  1. #1
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default NumberFormat is abstract?

    Hi yes, I am trying to format some numbers in java program but i get error message

    Documents\Java\PHotel.java:42: java.text.NumberFormat is abstract; cannot be instantiated
    NumberFormat nf = new NumberFormat();
    ^
    1 error

    Tool completed with exit code 1


    does anyone know whats going on here?
    I've got an idea--an idea so smart that my head would explode if I even began to know what I'm talking about.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Yes. Look at the Methods Summary sexction of the NumberFormat class api. It contains some static methods for obtaining instances of NumberFormat, eg,
    Java Code:
    NumberFormat nf = NumberFormat.getInstance();

  3. #3
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    I have NumberFormat nf = new NumberFormat();
    when I put NumberFormat nf = new NumberFormat.get Instance();
    i get error message

    Documents\Java\PHotel.java:17: cannot find symbol
    symbol : class getInstance
    location: class java.text.NumberFormat
    NumberFormat nf = new NumberFormat.getInstance();
    ^
    1 error


    CODE:

    import java.util.*;
    import java.io.*;
    import java.text.*;
    public class PHotel
    {
    public static void main(String[]args) throws InputMismatchException
    {
    int totalFloors;
    int numOfRooms;
    int roomsOccupied;
    int roomsVacant;
    double occupancyRate;


    NumberFormat nf = new NumberFormat();


    //Occupancy rate = number of rooms occupied/total number of rooms//
    System.out.println("How many floors are in the hotel?");
    Scanner keyboard = new Scanner(System.in);
    totalFloors = keyboard.nextInt();

    for(int i = 0; i!=totalFloors;i++)
    {
    if(totalFloors<=1)
    System.out.println("The building must have more then 1 floor.");
    if(totalFloors>1)
    System.out.println("How many rooms are on floor "+i+"?");
    numOfRooms = keyboard.nextInt();

    if(numOfRooms>=10)
    {
    System.out.println("How many rooms on floor "+i+" are occupied?");
    roomsOccupied = keyboard.nextInt();



    }
    else
    System.out.println("Each floor must have at least 10 rooms.");

    }

    System.out.println("Rooms in the hotel: "+numOfRooms);
    System.out.println("Rooms occupied: "+roomsOccupied);
    System.out.println("Rooms vacant: "+roomsVacant);
    System.out.println("occupancy rate:"+occupancyRate);
    }//end main
    }//end class
    I've got an idea--an idea so smart that my head would explode if I even began to know what I'm talking about.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I copied, pasted and compiled the code you posted above and got this:
    Java Code:
    C:\jexp>javac PHotelRx.java
    PHotelRx.java:16: java.text.NumberFormat is abstract; cannot be instantiated
            NumberFormat nf = new NumberFormat();
                              ^
    1 error
    Change this line
    Java Code:
            NumberFormat nf = new NumberFormat();
    to this
    Java Code:
            NumberFormat nf = NumberFormat.getInstance();
    Then give initial values, eg, 0, to the five variables declared at the beginning of the main method.

  5. #5
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    xcellent, ty
    I've got an idea--an idea so smart that my head would explode if I even began to know what I'm talking about.

Similar Threads

  1. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  2. Replies: 2
    Last Post: 11-21-2008, 12:20 AM
  3. Replies: 6
    Last Post: 10-27-2008, 12:25 AM
  4. Parsing a number using a NumberFormat
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-16-2008, 11:03 PM
  5. GUI for Abstract...
    By judepereira in forum AWT / Swing
    Replies: 0
    Last Post: 01-04-2008, 08:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •