Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Roman Numerals in a java string
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Anshu
Roman Numerals in a java string
1 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners



Advertisement
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,


Related Tutorials/Questions & Answers:
Roman Numerals in a java string - Java Beginners
Roman Numerals in a java string  Hi, I want to check a string... after repalcing roman numerals with space"); String newString=new String... and roman numerals. but its not fixed how th einput string will be. Here I have
Convert Number to Roman Number Using Java
Java Convert Number to Roman Numerals In this section, you will learn how to convert the number to roman number.  To do this, we have created a class named Roman in which we have stored some numbers and their roman numbers
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
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 - Java Beginners
java   /* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists * of a class, say Roman... * * The decimal values of the Roman numerals are * M 1000 * D 500 * C 100
Convert Roman Numbers to Decimal Numbers
("Enter a Roman Number: "); String roman = input.next(); String...Convert Roman Numbers to Decimal Numbers In the previous section, we have illustrated the conversion of Decimal Numbers to Roman. Here we are doing vice
java copilation - UML
in Roman numerals to decimal. Your program should consist of a class, say, Roman... values of the roman numerals are: M 1000 D 500 C 100 L 50 X 10 V 5 I 1 .../java/java-tips/45examples/misc/roman/roman.shtml http://www.roseindia.net
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
Java string
Java string  Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "Hello World". How
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
string - Java Beginners
string in java interview questions  What is string in java? Interview questions
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
Java string buffer
Java string buffer  what is stringBuffer and StringBuilder
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
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
String immutable - Java Beginners
String immutable  Why is String immutable?  See here Why String is immutable in Java
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
Java string buffer
Java string buffer  What is the basic difference between string and stringbuffer object
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
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
Core Java String
Core Java String  Hi , Here my code Class Test { String s1 = "null"; String s2 = "null"; String s3 = s1+s2; } what is the out put
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
On string - Java Beginners
On string -Display reverse String using string reverse method in Java  I wanted to display reverse String using string.reverse() method in Java  Display reverse String using string.reverse()String Reverse Using
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
String - Java Interview Questions
Java String array declaration and initialization  I am looking for an example of java string array declaration and initialization  Hipublic class ImmutableCart { private final List items; public ImmutableCart(List
String Mirror in Java
String Mirror in Java  how to get a mirror image of(string) a given string in java   Hello Friend, Try this: import java.util.*; class StringMirror{ public static void main(String []args){ String str="hello
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
Advertisements
 

 

 

DMCA.com