Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Roman Literals in Java

 
VarunKumar Mallisetty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When displaying the time in a clock,if i want the numbers displayed as roman literals like I,II,III,IV..What should i do.is there any available API which takes the numbers and returns the roman literals corresponding to them?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varun,
Welcome to JavaRanch!

There isn't an existing API to convert #s into Roman numerals, but it wouldn't be too hard for you to write one.

Mostly, there are mappings between numbers and symbols (5=V, 10=X, etc). Once you get one, you subtract that number and start over. Just watch for the exceptions (4,9,14,...)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only need values for the numbers 1-12, I think it would be pretty easy to just make an array with 12 different strings for the 12 different values you need to convert.

Then you can just look up values from the array, yes?

One issue to beware of here is that the "first" elemnt of this array is element 0, which maps to "I", and the "second" is element 1, which maps to "II". So you will need to do something to fix this minor off-by-one error...
 
VarunKumar Mallisetty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies..Perhaps there is no place like javaranch where we can find answers so quickly..

Now, i have one more question. What if i want to use other languages like german and languages which have different character set like hindi etc.
How should i proceed..i know that java supports all character sets because of UNICODE support, but dont know how to print characters in other character sets .
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you're asking a very different question. The question of using a German or other character set has nothing to do with Roman numerals. You can indeed use Java to show characters of any character set, as long as the required font is installed. E.g., something line Microsofts MS Arial Unicode font may be required to view Hindi, Chinese, Japanese or Korean characters. But be aware trhat many consoles can't handle Unicode, so System.out.println may not produce the results you expect.
 
VarunKumar Mallisetty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i did ask complete different question and thanks for the answer. But can you be more clear. I mean what API i have to use?. Is it just that i select the font installed in the system and use it?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to use any API - Java has all necessary capabilities built in. The important question is: where do you want Unicode characters to be displayed? Like I said, most consoles can't handle anything but ASCII, or maybe ISO-8859-1 if you're lucky, but not Hindi etc.

Web browsers can display those characters as long as you set the proper encoding (Content-Type should be "text/html; charset=utf-8") and an appropriate font is installed. Check this page to see which fonts you have.
[ July 06, 2006: Message edited by: Ulf Dittmer ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from console output or a web application viewable from a browser, the other main option you may have is to build a GUI application using Swing/AWT or SWT. As a quick & simple option, you could do something like this:

Many more elaborate & complex applications are possible. If console output is good enough, that's easiest. If you want to print German on computers running in Germany, that will probably work fine. If the computer is in China, maybe not - it's probably set up differently. Otherwise you probably need to decide whether you want a web application or a GUI. Either option will require you to learn some additional technology.
 
VarunKumar Mallisetty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I now understand better
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic