Roman Numerals in a java string

Ads
 

Roman Numerals in a java string

View Answers

June 19, 2008 at 6:50 PM

Hi friend,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class RomanExample extends JFrame {

JTextField tf1 = new JTextField(6);
JTextField tf2 = new JTextField(8);
public static void main(String[] args) {

RomanExample win = new RomanExample();
}

public RomanExample() {
JButton convertButton = new JButton("Convert");
ActionListener doIt = new ActionListener() {

public void actionPerformed(ActionEvent e) {
try {
int val = Integer.parseInt(tf1.getText());
String roman = Roman.int2roman(val);
tf2.setText("" + roman);
}
catch (NumberFormatException ex) {
tf2.setText("Error");
}
}};
convertButton.addActionListener(doIt);
tf1.addActionListener(doIt);
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(tf1);
content.add(convertButton);
content.add(tf2);
this.setContentPane(content);
this.setTitle("Decimal to Roman");
this.setSize(400,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

-------------------------
public class Roman {

final static romvalue[] ROMAN_VALUE_TABLE = {
new romvalue(1000, "M"),
new romvalue(900, "CM"),
new romvalue(500, "D"),
new romvalue(400, "CD"),
new romvalue(100, "C"),
new romvalue(90, "XC"),
new romvalue(50, "L"),
new romvalue(40, "XL"),
new romvalue(10, "X"),
new romvalue(9, "IX"),
new romvalue(5, "V"),
new romvalue(4, "IV"),
new romvalue(1, "I")
};

public static String int2roman(int n) {
if (n >= 4000 || n < 1) {
throw new NumberFormatException("Numbers must be in range 1-3999");
}
StringBuffer result = new StringBuffer(10);
for (romvalue equiv : ROMAN_VALUE_TABLE) {
while (n >= equiv.val) {
n -= equiv.val;
result.append(equiv.romVal);
}
}
return result.toString();
}

private static class romvalue {

int val;
String romVal;
romvalue(int dec, String rom) {
this.val = dec;
this.romVal = rom;
}
}
}
---------------------------------

Read for more information,

http://www.roseindia.net/java/

Thanks,

Ads









Related Tutorials/Questions & Answers:
ModuleNotFoundError: No module named 'roman-numerals'
ModuleNotFoundError: No module named 'roman-numerals'  Hi, My... named 'roman-numerals' How to remove the ModuleNotFoundError: No module named 'roman-numerals' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'roman-numerals-webservice'
ModuleNotFoundError: No module named 'roman-numerals-webservice'  Hi...: No module named 'roman-numerals-webservice' How to remove the ModuleNotFoundError: No module named 'roman-numerals-webservice' error? Thanks  
Advertisements
Java converts any given year to Roman Numeral
Java converts any given year to Roman Numeral  Write a program that converts any given year to Roman Numeral
Java String
Java String       In this section, you will learn about the java string. Java String is a set... to a string literal in the object stream. The Java String object contains a sequence
Write a program that converts any given year to Roman Numeral in Java
Write a program that converts any given year to Roman Numeral in Java  Write a program that converts any given year to Roman Numeral
Java String
Java String  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon as possible Thank you
Java String - Java Beginners
Java String  How to seperate the string into characters.  there is a method called getChars() in String class,plz use that and separate into characters so plz verify the String api for related questions 
String in Java - Java Beginners
]); } } } ---------------------------------------------------- I am sending simple code of String array. If you are beginner in java...String in Java  hiiiii i initialise a String array,str[] with a size... in advance   Hello Antony Insted Using String Array Use StringBuffer
How to split string in Java?
How to split string in Java?  Hi, I have a string seperated by ',' comma. How to split the string? Thanks
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
capitalizing string in java
capitalizing string in java  How to capitalizing string in Java?   String s = "some string"; String capitol = Character.toString(s.charAt(0)).toUpperCase(); String newString = capitol + s.substring(1,x
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do... the StringBuilder class. Check the example at Appending String in java. You can
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
Appending String efficiently in Java
for Appending String efficiently in Java. Thanks   Hi, StringBuilder class of Java API is used by developers of Java for appending string... example at How To Append String In Java? Thanks
Java String Datatype
Java String Datatype  Weather String in java is class or Datatype,if it's clss then How can i assign String str="hello";   String is a class found in the package java.lang.*. When we represent string in double quotes
java string vowel program
java string vowel program  java string vowel program   Please visit the following links: Display the word that begins with vowel Java Count Vowels
How to append String in Java?
How to append String in Java?  Hi, I have a number of string in Java which is to be appended efficiently. How to append String in Java? Thanks...: str.append("India"); You check complete tutorial at Appending String in Java
java programmming: string manipulation
java programmming: string manipulation  WAP to accept a string from...' occurring in it   Here is a java example that reads a string from the command prompt and find the frequency of the character e in the input string
java string multiplication
java string multiplication  Is there any simple method of string multiplication in java ..?   public class StringMultiplication { public static void main(String[] args) { String number1 = "17"; String number2
Java String searching.
Java String searching.  How to search a string in given String array... static void main(String[] args) { String city[] = { "Delhi", "Lucknow", "Mumbai", "Kanpur" }; String userCity; Scanner scanner = new
How To Append String In Java
How To Append String In Java In this section we will read about how to append String in Java. Here we will see the solution of this question through a simple example. There are various ways to append String in Java. Below, we
Core Java -String
Core Java -String  Write a program to print "Ranjan pintu am i". the given string is i am pintu ranjan. here there is a condition, we can't use split() method, using loop only
java string comparison example
java string comparison example  how to use equals method in String... static void main(String [] args){ String str="Rose"; String str1... using equals() method. In the above example, we have declared two string
String Array - Java Beginners
String Array  Thanks for the help you had provided,,, Your solution did worked... I worked on the previous solution given by you and it worked.... I... again,,, and I'll come back to you , if I had other problem regarding JAVA
Java String manipulation
Java String manipulation  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon
Java dom from string
Java dom from string  Hi, How to create dom tree from string data? I am trying to search the example for java dom from string. Thanks   ... UpdateData { public static void main(String[] args) { String url="http
Questions about Java's String pool
Questions about Java's String pool  Questions about Java's String pool
Java String operations
Java String operations  hi, please suggest me how to write the java...*; class ReadFileIgnoringSomeLines { public static void main(String[] args... StringBuffer(); String str; while((str=br.readLine())!=null
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer

Ads