Question: Roman Numerals in a java string

Ads

 
 
 

Share on Google+Share on Google+

Anshu
Roman Numerals in a java string
1 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
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:
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
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 - Java Beginners
string in java interview questions  What is string in java? Interview 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
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 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
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
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
Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads