Instantiating NumberFormat Class

  • Thread starter Thread starter friendbobbiny
  • Start date Start date
  • Tags Tags
    Class
Click For Summary
SUMMARY

The discussion centers on the inability to instantiate the abstract class java.text.NumberFormat in Java. The user attempted to create an instance using new NumberFormat(loc), which is not permissible. Instead, the correct approach is to utilize factory methods such as NumberFormat.getInstance() to obtain a concrete instance for formatting numbers based on the specified Locale.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with the java.text package
  • Knowledge of abstract classes in Java
  • Basic understanding of Locale in Java
NEXT STEPS
  • Learn how to use NumberFormat.getInstance() for number formatting
  • Explore the DecimalFormat class for advanced formatting options
  • Study the concept of abstract classes and their usage in Java
  • Investigate the Locale class and its role in internationalization
USEFUL FOR

Java developers, software engineers, and anyone working on applications that require number formatting and localization in Java.

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());
}
 
Technology 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:

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
4
Views
3K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
1
Views
2K