Instantiating NumberFormat Class

  • Thread starter Thread starter friendbobbiny
  • Start date Start date
  • Tags Tags
    Class
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
friendbobbiny
Messages
49
Reaction score
2
I've imported java.util, java.text and placed the following method under an appropriate class.

I'm unable to pinpoint why compiler can't instantiate the type NumberFormat in my method's second line





public static void tailorFormat(String name, double value, Locale loc){

NumberFormat kitty = new NumberFormat (loc); // <<<<< this is where I've referred to

DecimalFormat shelter = (DecimalFormat) kitty;
shelter.applyPattern(name);
shelter.format(value);
System.out.println(name + " " + value + " " + loc.toString());
}
 
Physics news on Phys.org
What is the error that the compiler is telling you?
Usually (but not always) the compiler is actually quite helpful in telling you what you've done wrong :)
 
java.text.NumberFormat is an abstract class, so you can't use it directly as you are trying to. Here's a sample of how this class can be used (see http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html):
To format a number for the current Locale, use one of the factory class methods:
Code:
  myString = NumberFormat.getInstance().format(myNumber);
The web page I linked to has several other examples that might be of help to you.
 
Last edited by a moderator: