-2

i am new to a java program and i have a homework where when i get input from a user as an string, i have to convert it to a double and multiple it with a .11111 using a switch decision structure.

A string input from user has to be a number in roman numeral which is I, II, III, IV, and so on....


//This is my program
package practiceNum;
import java.util.Scanner;

public class practiceNum{
    class stastic void main(String[] args){

    String num_In_Roman;    //declare a string

    Scanner keyboard = new Scanner(System.in);  //read input words

    System.out.println("Enter a roman numerals from I to X: "); //asking user input
    num_In_Roman = keyboard.nextLine();

    switch(num_In_Roman){

    case "I":
    break;

    case "II":
    break;

    //and so on until 10 (X) in roman numerals. 
    default:
        System.out.println("Invalid Number!");
    }

    //end of body program here
    }

}



Here's what i did to calculate my math but still giving me an error.

Double value = Double.parseDouble(num_In_Roman); //convert string into double

case "I":
value *= .111111;
System.out.println(value);
break;


someone help please.
Sategroup
  • 955
  • 13
  • 29
Lah Htoo
  • 11
  • 1
  • What is the error you are getting? And, I don't think Double would parse your Roman string... – Sategroup Sep 05 '15 at 02:38
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "I" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at sun.misc.FloatingDecimal.parseDouble(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at roman_Numerals.roman_Numerals.main(roman_Numerals.java:60) – Lah Htoo Sep 05 '15 at 02:41
  • There you have your answer. As I mentioned Roman strings are not parseable with Double – Sategroup Sep 05 '15 at 02:42
  • You have to figure out what number your Roman number translates into. This might be useful: http://stackoverflow.com/questions/9073150/converting-roman-numerals-to-decimal – Zarwan Sep 05 '15 at 02:43
  • Decoding roman numbers using a switch statement is a bad idea ... unless you know *for sure* that you only need to cover a tiny domain; e.g. "I" through "IX". – Stephen C Sep 05 '15 at 03:08

1 Answers1

1

This is the bad way, but this is what you can do:

case "I":
value = 1 * .111111;
System.out.println(value);
break;

case "II":
value = 2 * .111111;
System.out.println(value);
break;
Sategroup
  • 955
  • 13
  • 29