0

I'm trying to represent a square root in a string in Java, however ...

The Unicode for a square root, which is \u221A, just prints the symbol for a square root - I want it to wrap around my equation like so:

An example of a normal square root equation

But it comes out like this, as expected:

The Unicode symbol for a square root

How can I format my string to represent the square roots as such?

Thanks in advance!

  • 1
    I don't think you can have a straight `String` that looks like that. What are you actually trying to achieve? Are you just trying to draw it on the screen? Or make a PDF file? Or something different? – Dawood ibn Kareem Mar 30 '17 at 01:56
  • Actually, in Java String, the square root symbol shows like your second link. I think this link might helps you. http://stackoverflow.com/questions/15672161/print-a-square-root-symbol-%E2%88%9A-in-java – Junbang Huang Mar 30 '17 at 02:06
  • I'm trying to represent a string in an AlertDialog using Android Studio. For a mobile app. Should have clarified. – Jahnu Best Mar 30 '17 at 02:26
  • OK, if I had to do that, I'd probably just draw three lines directly on the window. I'd experiment a little bit to get the best looking lengths and angles. – Dawood ibn Kareem Mar 30 '17 at 02:38
  • Does it _have_ to look like it came out of a math textbook? A Java-like syntax with the square root symbol occupying the place of a method name is (I think) very readable: `√(3x + 1)`. – Kevin J. Chase Mar 30 '17 at 02:48
  • Ok. I'll try those. Thanks very much! – Jahnu Best Mar 30 '17 at 04:43

1 Answers1

2

If you mean you want to print this to the console then you cannot achieve what you are attempting to do - the closest you could achieve is an approximation of the overbar using underscores, eg:

 ___
√ x

If you aren't using the console and instead a rich rendering device (eg a Swing GUI, or a browser window) you may be able to achieve something richer in HTML using MathML (may require further JavaScript library support):

<math xmlns="http://www.w3.org/1998/Math/MathML">
    <msqrt>
        <mrow>x</mrow>
    </msqrt>
</math>

or by drawing it yourself into a suitable graphics context, eg a Graphics2D.

James Fry
  • 1,133
  • 1
  • 11
  • 28
  • Thanks for that! I should clarify, it's for a mobile app and the string is being represented in an AlertDialog box. And the IDE being used is Android Studio. – Jahnu Best Mar 30 '17 at 02:17