oopss.. sorry..^_^ this problem given to me requires me to have a constructor.. i know i could finish it without a construction.. but i must follow the rules.. anyway here it is.. its still incomplete though
import java.util.*;
public class ConvertRoman {
public static void main(String args[])throws Exception{
HashMap values = new HashMap();
values.put(new Character('M'), new Integer(1000));
values.put(new Character('D'), new Integer(500));
values.put(new Character('C'), new Integer(100));
values.put(new Character('L'), new Integer(50));
values.put(new Character('X'), new Integer(10));
values.put(new Character('V'), new Integer(5));
values.put(new Character('I'), new Integer(1));
Scanner kybd = new Scanner(System.in);
String number;
System.out.println("Input Roman or Arabic number to convert:");
System.out.println();
number = kybd.nextLine();
number = number.toUpperCase();
for(int x = 0; x < number.length();x++){
if((int)number.charAt(x) > 47 && (int)number.charAt(x)<58) {
RomanNum passval = new RomanNum(Integer.parseInt(number));
}
{
}
}
}
class RomanNum {
public RomanNum(int convert){
try
{if(convert > 3999 && convert < 1)
{
throw new NumberFormatException();
}
}catch(NumberFormatException a){
System.out.println("Integer value to big to convert to Roman Numeral!");
}
}
}
forgive my incompetence.. ^_^
the constructor is not a separate class.....and yes it is outside the main method...it is generally used to pass various arguments while initializing the class.......
as an example...
class Rectangle{
public Rectangle(int a){
}
public Rectangle(int a,String d){
}
public.....main....
{
///so now u can initialize in three ways......
Rectangle rect=new Rectangle();//this is default of course
Rectangle rect=new Rectangle(15);
Rectangle rect=new Rectangle(15,s);
}
}
You *can* put most anything in the constructor, but in this case it's wrong.
If a constructor completes normally, the resulting object should be in
a sensible state. But if I execute new RomanNum(12345), there's some
warning printed on the console, but the code continues normally.
That's a dsign error. Constructors are allowed to throw exceptions (hint).
public class Construct {
public Construct() {
try {
public void method() {
// code
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
like that? no. you can't create methods inside a method or a constructor.
i've gotten through the constructors and methods..
now, i am wondering if there is a way to find locate the key for a given HashMap value..
i used a HashMap for my conversion of Roman Numerals to Arabic
and vice versa.. but it seems my HashMap can only do Roman to Arabic..
HashMap values = new HashMap();
values.put(new Character('M'), new Integer(1000));
values.put(new Character('D'), new Integer(500));
values.put(new Character('C'), new Integer(100));
values.put(new Character('L'), new Integer(50));
values.put(new Character('X'), new Integer(10));
values.put(new Character('V'), new Integer(5));
values.put(new Character('I'), new Integer(1));
is there a way for me to get the KEY by giving the VALUE?
this has stomped me..
i am already able to convert Arabic Numbers to Roman numerals.. but im having hard time converting Roman Numerals to Arabic..
this is what ive got so far
static int toInteger(String roman){//my method that does the converting
int NewValue = 0;
HashMap values = new HashMap();
values.put(new Character('M'), new Integer(1000));
values.put(new Character('D'), new Integer(500));
values.put(new Character('C'), new Integer(100));
values.put(new Character('L'), new Integer(50));
values.put(new Character('X'), new Integer(10));
values.put(new Character('V'), new Integer(5));
values.put(new Character('I'), new Integer(1));
for(int x = 0; x < roman.length(); x++){
if(values.containsKey((Character)roman.charAt(x))) { //this is where it fails..
//it says that it cannot cast from char to Character..
//but the containsKey function needs
//an object parameter.. how do i go about that?
}
}
return NewValue;
}
thanks in advance^_^