• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7473
  • Last Modified:

How to display superscript in Java

I am preparing a calculator for  manipulation different units of Distance,Electricity etc.I want to add two values and display the result and the expression in a textbox.But the expression like m2(meter sqaure) cannot be displayed.Can anyone suggest a way to siplay supscript in Java
0
robinvt
Asked:
robinvt
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
InteractiveMindCommented:
import javax.swing.* ;
import java.awt.* ;

public class T extends JFrame
{
    public T()
    {
        super( "T" ) ;
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
       
        getContentPane().add( new JTextField( "m²" ) ) ;
       
        pack() ;
        setLocationRelativeTo( null ) ;
       
        setVisible( true ) ;
    }
   
    public static void main( String [] a )
    {
        new T() ;
    }
}
0
 
InteractiveMindCommented:
What's the problem exactly?
0
 
nayernaguibCommented:
You can use the characters \u00b2 and \u00b3 if you need to display only powers of two and three.

_______________

  Nayer Naguib
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
InteractiveMindCommented:
Or, as I've demonstrated above, just type in the characters as alt code into the source (seeing as the Java compiler supports Unicode).
0
 
nayernaguibCommented:
For example, "44 m\u00b3" is equivalent to "44 m³".

_______________

  Nayer Naguib
0
 
robinvtAuthor Commented:
The advice given by nayernaguib is working for me.But its not working for A-3(A raise to -3) s-6, A-1 kind of expressions.can u pls advice.
InteractiveMind also gave the valuable suggestion but i am using simple TextField in Java to display it.
0
 
nayernaguibCommented:
You can use the character \u02c9 or \u203e (althought both were not intended to represent a superscript minus sign).
For example, the string "44 m\u02c9\u00b3" is equivalent to "44 mˉ³".
Depending on the font you are using, you will be able to determine which character looks more like a superscript minus sign.

_______________

  Nayer Naguib
0
 
BigRatCommented:
Goto www.unicode.org and search for the plane "Superscripts and Subscripts" which are in the area staring at 2070*. (http://www.unicode.org/charts/PDF/U2070.pdf) You have to use B9 for superscript 1, B2 for superscript 2 and B3 for superscript 3, but the rest 0-9 can be made by adding the number to 2070. The subscripts start at 2080 - just add the number. Other super/subscripted characters are +,-,=,( and ) starting in that order at 207A and 208A. The character 207F is a superscript n.

* all numbers are in hexadecimal.
0
 
robinvtAuthor Commented:
The inputs gave by nayernaguib  is the answer expected by me.So i would like to award 75 points while  inputs gave by BigRat is also worth while.I would like to ward 50 points for that.
Thanks for all who gave their comments
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now