java

Java Lab Project


So I am currently working on a lab for class. I'll attach the lab below:

For this lab, you will use a series of nested if-else or if-else-if statements in order to convert a Roman Numeral number into its String word form. The numerals we are concerned with are I, II, III, IV, V, VI, VII, VIII, which coincide with One, Two, Three, Four, Five, Six, Seven, Eight. Numbers outside of this range are denied – you must tell the user that they are denied as input.

Your code must not fall for inputs like ‘I am the best roman numeral, ‘IIIlikefish’, or ‘VIII is delicious.’ Those inputs should be denied just like an input outside of I to VIII would be

What are possible ways to avoid falling for fake inputs?

Also, I currently have 8 different if statements, is there a faster way to do this?

import java.util.Scanner;

public class RomanNumeralChecker {

   public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);

      String romanNum;

      System.out.println("Please enter a Roman Numeral between the values 1 and 8:");
      romanNum = keyboard.next();

      if (romanNum.equalsIgnoreCase("I")){
         System.out.println(romanNum + " represents the number \"One\"");
      }
      else if (romanNum.equalsIgnoreCase("II")){
         System.out.println(romanNum + " represents the number \"Two\"");
      }
      else if (romanNum.equalsIgnoreCase("III")){
         System.out.println(romanNum + " represents the number \"Three\"");
      }
      else if (romanNum.equalsIgnoreCase("IV")){
         System.out.println(romanNum + " represents the number \"Four\"");
      }
      else if (romanNum.equalsIgnoreCase("V")){
         System.out.println(romanNum + " represents the number \"Five\"");
      }
      else if (romanNum.equalsIgnoreCase("VI")){
         System.out.println(romanNum + " represents the number \"Six\"");
      }
      else if (romanNum.equalsIgnoreCase("VII")){
         System.out.println(romanNum + " represents the number \"Seven\"");
      }
      else if (romanNum.equalsIgnoreCase("VIII")){
         System.out.println(romanNum + " represents the number \"Eight\"");
      }
      else {
         System.out.println("Sorry, but " + romanNum + " is out of the desired range.");
      }
   }
}

Solution

  • You can reduce the number of if statement by using Map.

    import java.util.Scanner;
    import java.util.*;
    
    public class RomanNumeralChecker {
        public static void main(String[] args) {
    
            Map<String, String> m = new HashMap<String, String>();
            m.put("I", "One");
            m.put("II", "Two");
            m.put("III", "Three");
            m.put("IV", "Four");
            m.put("V", "Five");
            m.put("VI", "Six");
            m.put("VII", "Seven");
            m.put("VIII", "Eight");
    
            System.out.println("Please enter a Roman Numeral between the values 1 and 8:");
    
            Scanner keyboard = new Scanner(System.in);
            String romanNum = keyboard.next();
    
            String num = m.get(romanNum);
            if (num != null) {
                System.out.println(romanNum + " represents the number \"" + num + "\"");   
            } else {
                System.out.println("Sorry, but " + romanNum + " is out of the desired range.");
            }
        }
    }