import java.util.Scanner;
import java.util.Arrays;
/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

public class Main
{
       public static void main(String[] args) 
       {
           System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
            char[] acceptedNumerals = new char[]{'M','D','C','L','X', 'V','I'};
         
            int number;
     
            boolean noMatch=true;
        
            int j;
            int count=0;
        
            Scanner reader=null;
        
            reader = new Scanner(System.in);
            System.out.println("Enter number numerals in the number to be converted to decimal:");
            number=reader.nextInt();
        
            char inputtedNumerals[] = new char[number]; 
            int [][] posValues = new int [number][2];
        
            do
            {
                reader = new Scanner(System.in);
                System.out.println("Enter roman numeral " + (count+1) + " of " + number + ":");   // end user prompted to enter roman numeral
                inputtedNumerals[count]=reader.next().charAt(0);
                count++;
                
                for (j=0; j<inputtedNumerals.length;j++)
                {
                    for (int k=0; k<acceptedNumerals.length;k++) 
                    {
                        if (inputtedNumerals[j]==acceptedNumerals[k])
                        {
                            posValues[count-1][0]=k;
                            
                            switch(k)
                            {
                                case 0:
                                    posValues[count-1][1]=1000;
                                break;
            
                                case 1:
                                    posValues[count-1][1]=500;
                                break;
                                
                                case 2:
                                    posValues[count-1][1]=100;
                                break;
                
                                case 3:
                                    posValues[count-1][1]=50;
                                break;
                                
                                case 4:
                                    posValues[count-1][1]=10;
                                break;
                                
                                case 5:
                                    posValues[count-1][1]=5;
                                break;
                                
                                case 6:
                                    posValues[count-1][1]=1;
                                break;
                            }
        
                            noMatch=false;   // This sets flag stating match has been found.
                            break;
                        }
                    }

                     if (noMatch==true)
                    {
                        System.out.println("Please input a successful roman numeral:  M  D  C  L  X  V  I");
                        count=count-1;
                        break;
                    }
                }
            } while (count<inputtedNumerals.length);  // do while loop will end once count reaches the input user for length of roman numerals.
            
            String str = String.valueOf(inputtedNumerals);
            System.out.println("This will be converted to decimal:" + str);  // This informs end user of the roman numerals to be converted

            performConversion pc = new performConversion(acceptedNumerals, inputtedNumerals,posValues,str); // calls constructor in class to perform conversion
    }
}

class performConversion
{
    public void getLogs()
    {
        for (String s: errorLog)
        {
            if (s!=null)
            {
                System.out.println(s);
                
            }
        }
    }
    
    int logCounter;
    
    String errorLog[] = new String[30];
    
    boolean validNumber=false;
    boolean flagSet=false;
    int tempA=0;
    int tempB=0;
    
    int sumNotAdded=0;
    int rule4;
    boolean rule3Fail=false;
    char[] inputtedNumerals; 
    
    char[] acceptedNumerals; 
    int [][] posValues;
    int total; 
    int sum;
    int consecutiveOccurrences; 
    String numeralsToString;
    boolean correctOrder=false;
    
    int oneNumeral;
    int twoNumerals;
    boolean illegalSubtractive=false;
    boolean illegalFourInARowState=false;
    int temp1=0;
    boolean valueBeforeSubtractiveNotation=false;
    int runningTotal=0;
     
    public performConversion (char[] aNumerals, char [] inputNumerals, int [][] pValues, String numeralsToString)
    {
        
        int pc[] = new int[pValues.length];
        this.acceptedNumerals=aNumerals;
        this.inputtedNumerals=inputNumerals;
        this.posValues=pValues;
        this.numeralsToString=numeralsToString;
        
        converter(pc);
    }
    public void converter(int [] processedConversion)
    {
        System.out.println("the length is:" + inputtedNumerals.length);
        if (inputtedNumerals.length<2)    // This functions correctly.
        {
                System.out.println("Conversion is single:" + posValues[0][1]);
                
        }  
        
        System.out.println("numerals in the roman numeral:" + posValues.length);
        
        int valueConsecutiveOccurences[] = new int [posValues.length];
        int indexCount=0;
        int countV=0;   
        int countL=0;
        int countD=0;
        int zeroIndexCount=0;
        int rule4Single=0;
        int rule5=0;
        int rule5total;
        boolean rule5State=false;
        int rule6=0;
        int rule6total;   
        boolean rule6State=false;
        int rule7=0;
        int rule7total;
        boolean rule7State=false;
        String IV =  "IV";
        String IX =  "IX";
        String XL =  "XL";
        String XC =  "XC";
        String CD=   "CD"; 
        String CM=   "CM";
        
        int threeInRow=0;
        
        String [] subtractiveNumerals =  new String []{IV,IX,XL,XC,CD,CM};
        
        int [][] subtractiveNumeralsRange = new int[6][2];
        
        subtractiveNumeralsRange[0][0] = 1;   subtractiveNumeralsRange[0][1] = 4;
        subtractiveNumeralsRange[1][0] = 1;   subtractiveNumeralsRange[1][1] = 9;
        subtractiveNumeralsRange[2][0] = 10;   subtractiveNumeralsRange[2][1] = 40;
        subtractiveNumeralsRange[3][0] = 10;   subtractiveNumeralsRange[3][1] = 90;
        subtractiveNumeralsRange[4][0] = 100;   subtractiveNumeralsRange[4][1] = 500;
        subtractiveNumeralsRange[5][0] = 100;   subtractiveNumeralsRange[5][1] = 900;
        
        int sum1=0;
        int total1=0;
        boolean flagError=false;
        boolean result=false;
        int total2=0;
        
        boolean twoSubtractiveNotations=false;
        boolean singleSubtractiveNotations=false;
        boolean IplacedIncorrect=false;
        
        boolean rule2State=false;
        int tempBefore=0;
        
        int rule2=0;
        int rule2total=0;
        boolean invalidSubtractiveNumeral=false;
        int temp=0;
        
        boolean VLDcheck=false;
        
        boolean illegalThreeInARowState=false;
        int rangeLower=0;
        int rangeUpper=0;
        
        String checkComplete[] = new String[subtractiveNumerals.length];
        
        System.out.println("In rule 4");
        for (int i=0; i<subtractiveNumerals.length; i++) 
            System.out.println("***********************");
            
            for (int j=0; j<subtractiveNumerals.length; j++ ) 
            {
                if (j==i)
                {
                    continue;
                }
                
                if (numeralsToString.indexOf(subtractiveNumerals[i])!=-1 &&  numeralsToString.indexOf(subtractiveNumerals[j])!=-1) 
                {
                     twoSubtractiveNotations = true;
                     flagSet=true;
                     System.out.println("TWO SUBTRACTIVE NOTATIONS FOUND:");
                     System.out.println("***************************");
                    
                    if (numeralsToString.indexOf(subtractiveNumerals[i]) < numeralsToString.indexOf(subtractiveNumerals[j]))
                    
                    {
                        checkComplete[i]=subtractiveNumerals[i]+subtractiveNumerals[j];
                        
                        if(checkComplete[i].indexOf(subtractiveNumerals[j])==-1)
                        {
                        
                        System.out.println("WHY");
                        System.out.println(numeralsToString.indexOf(subtractiveNumerals[i]));
                        System.out.println((subtractiveNumerals[i]));
                        
                        System.out.println(numeralsToString.indexOf(subtractiveNumerals[j]));
                        System.out.println((subtractiveNumerals[j]));
                        
                        
                        System.out.println(numeralsToString + " is invalid");
                        System.out.println("Incorrect roman numeral. " +  subtractiveNumerals[i] + " portion should be before: " + subtractiveNumerals[j]);
                        
                        errorLog[logCounter]="A group of numerals written in subtractive notation, of lower value, " +  subtractiveNumerals[i] + " ( =" + subtractiveNumeralsRange[i][1]+"), cannot precede another group of numerals written in subtractive notation," + subtractiveNumerals[j] + " ( =" + subtractiveNumeralsRange[j][1]+")."; 
                        
                        logCounter++;
                        
                        illegalSubtractive=true;
                        invalidSubtractiveNumeral=true;
                        
                        }
                    }
                
                    if (numeralsToString.indexOf(subtractiveNumerals[i]) > numeralsToString.indexOf(subtractiveNumerals[j]))
                    {
                        System.out.println(subtractiveNumerals[i]);    
                        System.out.println("This should print out start of the range:" + subtractiveNumeralsRange[i][0]);
                        System.out.println("This should print out end of the range:" + subtractiveNumeralsRange[i][1]);
                
                        System.out.println(subtractiveNumerals[j]); 
                        System.out.println("This should print out start of the range:" + subtractiveNumeralsRange[j][0]);
                        System.out.println("This should print out end of the range:" + subtractiveNumeralsRange[j][1]);
                    
                        if (subtractiveNumeralsRange[i][0] == subtractiveNumeralsRange[j][0])  // these subtractive notations are in the same range
                        {
                            System.out.println(subtractiveNumerals[i] + " is in the range: " + subtractiveNumeralsRange[i][0] + " - " + subtractiveNumeralsRange[i][1]); 
                            System.out.println(subtractiveNumerals[j] + " is in the range: " + subtractiveNumeralsRange[j][0] + " - " + subtractiveNumeralsRange[j][1]);
                            System.out.println("They are both in the same range");
                        
                        
                            processedConversion[numeralsToString.indexOf(subtractiveNumerals[i])]=1;
                            temp = numeralsToString.indexOf(subtractiveNumerals[i]) + 1;
                            processedConversion[temp]=1;
                        
                            System.out.println("REACH HERE!!!!!!!!!!");
                            processedConversion[numeralsToString.indexOf(subtractiveNumerals[j])]=1;
                            temp = numeralsToString.indexOf(subtractiveNumerals[j]) + 1;
                            processedConversion[temp]=1;
                        
                            rule4=0;
                        }
                    
                    if (subtractiveNumeralsRange[i][0] != subtractiveNumeralsRange[j][0])  // this ensures that subtractive notations are not in the same range
                    {
                        
                        if(processedConversion[numeralsToString.indexOf(subtractiveNumerals[i])]!=1 && processedConversion[numeralsToString.indexOf(subtractiveNumerals[j])]!=1 )
                        {
                    
                            System.out.println("1---------------------------------------runningTotal IS: " +  runningTotal);
                    
                            if (numeralsToString.indexOf(subtractiveNumerals[i])!=-1 &&  numeralsToString.indexOf(subtractiveNumerals[j])!=-1)
                            {
                                System.out.println("correct order");
                                correctOrder=true;
                        
                                System.out.println(subtractiveNumerals[i] + "   " + numeralsToString.indexOf(subtractiveNumerals[i]));
                                System.out.println(subtractiveNumerals[j] + "   " + numeralsToString.indexOf(subtractiveNumerals[j]));
                                
                                processedConversion[numeralsToString.indexOf(subtractiveNumerals[i])]=1;
                                temp = numeralsToString.indexOf(subtractiveNumerals[i]) + 1;
                                processedConversion[temp]=1;
                        
                                processedConversion[numeralsToString.indexOf(subtractiveNumerals[j])]=1;
                                temp = numeralsToString.indexOf(subtractiveNumerals[j]) + 1;
                                processedConversion[temp]=1;
                        
                                switch (subtractiveNumerals[i])
                                {
                                    case "IV":
                                        runningTotal = runningTotal + 4;
                                    break;
                                
                                    case "IX":
                                        runningTotal = runningTotal + 9;
                                    break;
                                
                                    case "XL":
                                        runningTotal = runningTotal + 40;
                                    break;
                                
                                    case "XC":
                                        runningTotal = runningTotal + 90;
                                    break;
                                
                                    case "CD":
                                        runningTotal = runningTotal + 400;
                                    break;
                                
                                    case "CM":
                                        runningTotal = runningTotal + 900;
                                    break;
                                
                                    default:
                                    invalidSubtractiveNumeral = true;
                                }
                                
                                switch (subtractiveNumerals[j])
                                {
                                    case "IV":
                                        runningTotal = runningTotal + 4;
                                    break;
                                
                                    case "IX":
                                        runningTotal = runningTotal + 9;
                                    break;
                                
                                    case "XL":
                                        runningTotal = runningTotal + 40;
                                    break;
                                
                                    case "XC":
                                        runningTotal = runningTotal + 90;
                                    break;
                                
                                    case "CD":
                                        runningTotal = runningTotal + 400;
                                    break;
                                
                                    case "CM":
                                        runningTotal = runningTotal + 900;
                                    break;
                                
                                    default:
                                        invalidSubtractiveNumeral = true;
                                }
                            }
                        }
                    }
                }
            }
        }
            
            System.out.println("2---------------------------------------runningTotal IS: " +  runningTotal);
            System.out.println(subtractiveNumerals[i] + "   " + numeralsToString.indexOf(subtractiveNumerals[i]));
            boolean correctOrder=false;
            System.out.println("total after rule4:" + rule4);
            System.out.println(twoSubtractiveNotations);
               
            if (!twoSubtractiveNotations)
            {
                System.out.println("What is I:" + i);
                System.out.println("BE HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                
                if (numeralsToString.indexOf(subtractiveNumerals[i])!=-1)
                {
                    System.out.println("BE HERE AGAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                    System.out.println("BE HERE AGAIN2!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                    
                    System.out.println("ASDASDBGDFSDGFDGFDG");
                    System.out.println("BEFORE: " + runningTotal);
                    //System.out.println("BEFORE untotal: " + unTotal);
                    System.out.println(subtractiveNumerals[i]);
                    
                    runningTotal = runningTotal + subtractiveNumeralsRange[i][1];
                    
                    
                    System.out.println("AFTER: " + runningTotal);
                    
                    if (numeralsToString.indexOf(subtractiveNumerals[i])!=-1)  
                    {
                        System.out.println(numeralsToString.indexOf(subtractiveNumerals[i]));
                    
                        if (numeralsToString.indexOf(subtractiveNumerals[i])>=0 && numeralsToString.indexOf(subtractiveNumerals[i]) + 2<=posValues.length-1)
                        {
                            tempA = numeralsToString.indexOf(subtractiveNumerals[i])+2;
                            System.out.println("NUMERAL AFTER NOTATION!");
                        }
                    
                        if (numeralsToString.indexOf(subtractiveNumerals[i])!=0)
                        {
                            tempB = numeralsToString.indexOf(subtractiveNumerals[i])-1;
                            valueBeforeSubtractiveNotation = true;
                            tempBefore= numeralsToString.indexOf(subtractiveNumerals[i]);
                            System.out.println("NUMERAL BEFORE NOTATION!");
                        }
                    
                        System.out.println("vcbvbnm,,");
                        System.out.println(posValues.length-1);
                        System.out.println(numeralsToString.indexOf(subtractiveNumerals[i]));
                     
                        System.out.println("first");
                    }
                    
                    if (numeralsToString.indexOf(subtractiveNumerals[i])!=-1 && posValues.length==2)
                    {
                        temp = numeralsToString.indexOf(subtractiveNumerals[i]) + 1;
                        System.out.println("second");
                    }
                     
                     System.out.println("temp is currrentl: " + temp);
                     System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWW");
                     System.out.println("What is I:" + i);
                     
                     if (temp<=posValues.length && posValues.length>2 )
                     {   
                         System.out.println("33---------------------------------------runningTotal IS: "  + runningTotal);
                         
                         switch (subtractiveNumerals[i].charAt(0))
                         {
                             case 'I':
                                     rangeLower=1;
                                     break;
                                     
                            case 'V':
                                rangeLower=5;
                                     break;
                                     
                            case 'X':
                                rangeLower=10;
                                     break;
                                     
                            case 'L':
                                rangeLower=50;
                                     break;
                                     
                            case 'C':
                                rangeLower=100;
                                     break;
                                     
                            case 'D':
                                rangeLower=500;
                                     break;
                                     
                            case 'M':
                                rangeLower=1000;
                                     break;
                            
                            default:
                            System.out.println("Do not reach here");
                        }
                         
                        switch (subtractiveNumerals[i].charAt(1))
                        {
                             case 'I':
                                     rangeUpper=1;
                                     break;
                                     
                            case 'V':
                                rangeUpper=5;
                                     break;
                                     
                            case 'X':
                                rangeUpper=10;
                                     break;
                                     
                            case 'L':
                                rangeUpper=50;
                                     break;
                                     
                            case 'C':
                                rangeUpper=100;
                                     break;
                                     
                            case 'D':
                                rangeUpper=500;
                                     break;
                                     
                            case 'M':
                                rangeUpper=1000;
                                     break;
                            
                            default:
                            System.out.println("Do not reach here");
                             
                         }
                         
                         System.out.println(subtractiveNumerals[i].charAt(0));
                         System.out.println("IVC why not rule 2");
                         
                         System.out.println(tempBefore);
                         System.out.println(rangeUpper);
                         System.out.println(rangeLower);
                         System.out.println(zeroIndexCount);
                         System.out.println(tempB);
                         System.out.println(tempA);
                         
                    if (posValues[tempB][1] <(subtractiveNumeralsRange[i][1]) && tempBefore>tempB)
                    {
                        System.out.println("%*%*%*");
                        
                        errorLog[logCounter]="2. A numeral (a letter) of lower value " + numeralsToString.charAt(tempB) +  "(=" + posValues[tempB][1] + ") "+  "cannot precede a group of numerals written in subtractive notation, " + subtractiveNumerals[i] + "( ="+ subtractiveNumeralsRange[i][1] +").";
                        
                        logCounter++;
                        
                        illegalSubtractive=true;
                    }
                    
                    System.out.println("Rule 1..... values");
                    System.out.println(posValues[zeroIndexCount][1]);
                    System.out.println(rangeLower);
                    System.out.println(rangeUpper);
                    System.out.println(tempBefore);
                    System.out.println(tempA);
                    System.out.println(tempB);
                    
                    System.out.println("What is I: " +i);
                    
if (posValues[tempA][1] >=subtractiveNumeralsRange[i][0] && posValues[tempA][1] <=subtractiveNumeralsRange[i][1] && tempA>tempBefore)  /*&& posValues.length>2*/
                    {
                        System.out.println("%*%*%*");
                        
                        
                        errorLog[logCounter]="1. The numeral " + numeralsToString.charAt(tempA) + "(=" + posValues[tempA][1] + ") " + " cannot be placed after a group of numerals written in subtractive notation: " + subtractiveNumerals[i] +" of the same range: " +  "( = " + subtractiveNumeralsRange[i][0] + "-" + subtractiveNumeralsRange[i][1] + ").";
                        
                        logCounter++;
                        illegalSubtractive=true;
                    }
                    
                    System.out.println("TempBefore:" + tempBefore);
                    
                    System.out.println(subtractiveNumeralsRange[i][1]);
                    
                    System.out.println("this location-----------");
                    System.out.println(subtractiveNumeralsRange[i][1]);
                    System.out.println(tempBefore);
                    System.out.println(temp);
                    System.out.println(zeroIndexCount);
                    System.out.println("YOU ARE NOW HERE*****************************************************************************************");
                    
                    System.out.println(subtractiveNumeralsRange[i][1]);
                    System.out.println(posValues[temp][1]);
                    System.out.println(temp);
                    System.out.println(tempBefore); 
                    
                    if (subtractiveNumeralsRange[i][1] < posValues[tempA][1] && tempBefore<tempA)  /*&& posValues.length>2*/
                    {
                        System.out.println("%*%*%*");
                        
                        errorLog[logCounter] = "3ver1. A group of numerals written in subtractive notation, of lower value, "  + subtractiveNumerals[i] + " ( = " + (rangeUpper-rangeLower) + "), cannot precede a numeral of larger value, "   +  numeralsToString.charAt(tempA) + "( = "+posValues[tempA][1]+".";   
                        logCounter++;
                        
                        illegalSubtractive=true;
                    }
                    
                    if (subtractiveNumeralsRange[i][1] > posValues[tempB][1] && tempBefore>tempB)
                    {
                        System.out.println("%*%*%*");
                        
                        errorLog[logCounter]="3ver2. A group of numerals written in subtractive notation, of higher value, " + subtractiveNumerals[i] + " ( = " + (rangeUpper-rangeLower) + "), cannot proceed a numeral of lower value, "   +  numeralsToString.charAt(tempB) + "( = "+posValues[tempB][1]+".";
                        
                        logCounter++;
                        illegalSubtractive=true;
                    }
                        switch (subtractiveNumerals[i])
                        {
                            case "IV":
                                rule4Single = rule4Single + 4;
                                break;
                                
                            case "IX":
                                rule4Single = rule4Single + 9;
                                break;
                                
                            case "XL":
                                rule4Single = rule4Single + 40;
                                break;
                                
                            case "XC":
                                rule4Single = rule4Single + 90;
                                break;
                                
                            case "CD":
                                rule4Single = rule4Single + 400;
                                break;
                                
                            case "CM":
                                rule4Single = rule4Single + 900;
                                break;
                            
                            default:
                            invalidSubtractiveNumeral = true;
                        }
                        
                        System.out.println("Does it even get here");
                        
                        System.out.println("It's conversion is:" +rule4Single);
                        twoSubtractiveNotations=false;
                        singleSubtractiveNotations=true;
                    }
                
                    processedConversion[numeralsToString.indexOf(subtractiveNumerals[i])]=1;
                    temp = numeralsToString.indexOf(subtractiveNumerals[i]) + 1;
                    processedConversion[temp]=1;
                }
            }
        }
       
        for (int m=1; m<posValues.length; m++)  // this is starting not at zero index since compared to previous value
        {
            if (processedConversion[m]!=1)
            {
                System.out.println("Going into rule 3");
            
                if (!VLDcheck)
                {
                    for (indexCount=0; indexCount<posValues.length; indexCount++)
                    {
                        System.out.println("value of indexCount: " + indexCount);
                        System.out.println(posValues.length);
                
                        if (posValues[indexCount][1]==5)
                        {
                            countV++;
                            System.out.println("found a V");
                        }
                        
                        if (posValues[indexCount][1]==50)
                        {
                            countL++;
                        }
                        
                        if (posValues[indexCount][1]==500)
                        {
                            countD++;
                        }
                    }
                }
            
                VLDcheck=true;
                
                if (posValues[m][1]==posValues[m-1][1])   // this is comparing decimal number for numeral against previous one
                {
                    System.out.println("starting rule 1");
                 
                    if (posValues.length>3)
                    {
                        if (m<=posValues.length-3)  // can not check for three in row if last element in array
                        {
                            System.out.println("start of rule2");
                            System.out.println("********************************************************");
                            System.out.println("This is temp1:" + temp1);
                            System.out.println("********************************************************");
                            System.out.println("££££££££");
                            System.out.println(temp1);
                            System.out.println("XSEDXSED");
                            System.out.println(temp1+3);
                            System.out.println(posValues.length);
                        
                            if (temp1+3<=posValues.length)
                            {
                                for (int counter=0;counter<=posValues.length-3;counter=counter+4)
                                {
                                    System.out.println("Value of coutner: " + counter);
                        
if (posValues[temp1][1]==posValues[temp1+1][1] && posValues[temp1][1]==posValues[temp1+2][1]  &&  posValues[temp1][1]==posValues[temp1+3][1])
                                    {
                                    
                                    System.out.println("Four in a row occurrence of: ");
                                    illegalFourInARowState=true;
                        
                        
                                    System.out.println("********************************************************");
                                    System.out.println("This is temp1 99:" + temp1);
                                    System.out.println("********************************************************");
                        
                                    threeInRow = (posValues[temp1][1] * 3) +threeInRow;
                        
                                    System.out.println(threeInRow);
                                    
                                    temp1 = temp1+4;
                        
                                    System.out.println("********************************************************");
                                    System.out.println("This is new temp1 :" + temp1);
                                    System.out.println("********************************************************");
                    
                                    if (temp1+3>posValues.length)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                
                if (!illegalFourInARowState)
                {
                    System.out.println("Amit Amlani");
                    
                    if (processedConversion[m]!=1 && processedConversion[m-1]!=1)
                    {
                        rule2total=posValues[m][1]+posValues[m-1][1];
                        rule2=rule2+rule2total; 
                        System.out.println("So far sum:" + rule2);
                        rule2State=true;
                    }
                    
                    valueConsecutiveOccurences[m] = posValues[m][1]; 
                    
                    consecutiveOccurrences=consecutiveOccurrences+2;
                    
                    if (m!=(posValues.length-1))
                    {
                                            
                    if (valueConsecutiveOccurences[m]==posValues[m+1][1] && processedConversion[m+1]!=1)
                    {
                        
                        System.out.println("does this happen45");
                        rule2=rule2-(rule2total/2);
                        rule2total=0;
                    }
                }
                
                if (illegalThreeInARowState)
                {
                    rule2=0;
                }
            }
        }
        
        System.out.println("What is value of zeroindexcount: " + zeroIndexCount);
                
        if (posValues[m][1]<posValues[m-1][1])
        {
            System.out.println("How many times does it enter in this section");
                    
            System.out.println("What is m: " + posValues[m][1]);
            System.out.println("What is m-1: " + posValues[m-1][1]);
                   
                    
                    //System.out.println("This is value marked with 1: " +  posValues[processedConversion[numeralsToString.indexOf(subtractiveNumerals[m])][1]]);
                    
                    //********************Need to be careful here of making it already set to 1
                    //
                    
                    //System.out.println("This is value marked with 1: " +  posValues[processedConversion[numeralsToString.indexOf(subtractiveNumerals[m])-1][1]]);
                    
                    if (processedConversion[m-1]!=1)
                    {
                        
                    rule5total = posValues[m][1] + posValues[m-1][1];
                    rule5 = rule5total + rule5;
                    System.out.println("*******RULE5******: " + rule5);
                    rule5total = 0;
                    rule5State=true;
                    //processedConversion[m]=1;
                    
                    //UNSURE
                    //processedConversion[m-1]=1;
                    
                    
                    if (posValues.length==2)
                    {
                        //processedConversion[m-1]=1;
                    }
                    
                    if (m!=1)   // if not the first numeral
                    {
                        System.out.println("ARE WER HERE******");
                        System.out.println(m);
                        
                    
                    if (rule4Single!=0)
                    {
                        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!");
                        //rule5=rule4Single + posValues[m][1];
                        rule5=posValues[m][1] + posValues[m-1][1];
                    }
                    
                    
                }
                    
                }
                }
                System.out.println("at rule 6: " + total1);
                
               //Rule 6: When a Roman numeral is placed before another Roman numeral of greater value, 
               //the result is the difference between the numerals. For example, IV = 5 - 1 = 4, or, XL = 50 - 10 = 40, 
               //or XC = 100 - 10 = 90
                
                System.out.println("Rule 6");
                System.out.println("Must WORK!!!!");
                
                System.out.println(zeroIndexCount);
                System.out.println(posValues[zeroIndexCount][1]);
                
                if (posValues[zeroIndexCount][1]<posValues[zeroIndexCount+1][1])
                {
                    if (posValues[zeroIndexCount][1]==1 && posValues[zeroIndexCount+1][1]>10)
                    {
                        errorLog[logCounter]="Roman Numeral I can not precede: " + posValues[zeroIndexCount+1][1];
                        logCounter++;
                        IplacedIncorrect=true;
                    }
                    
                    if (posValues[zeroIndexCount][1]==10 && posValues[zeroIndexCount+1][1]>100)
                    {
                        errorLog[logCounter]="Roman Numeral X can not precede: " + posValues[zeroIndexCount+1][1];
                        logCounter++;
                        IplacedIncorrect=true;
                    }
                    
                    if (posValues[zeroIndexCount][1]==50 && posValues[zeroIndexCount+1][1]>=50)
                    {
                         errorLog[logCounter]="Roman Numeral L can not precede: " + posValues[zeroIndexCount+1][1];
                        logCounter++;
                        
                        
                        
                        IplacedIncorrect=true;  
                    }
                    
                    if (posValues[zeroIndexCount][1]==5 && posValues[zeroIndexCount+1][1]>posValues[zeroIndexCount][1])
                    {
                        errorLog[logCounter]="Roman Numeral V can not precede: " + posValues[zeroIndexCount+1][1];
                        
                        logCounter++;
                        
                        IplacedIncorrect=true;  
                    }
                    
                    
                    if (posValues[zeroIndexCount][1]!=5 && posValues[zeroIndexCount][1]!=50 && posValues[zeroIndexCount][1]!=500 && !IplacedIncorrect)
                    
                    {
                        
                  
                    processedConversion[zeroIndexCount]=1;
                    
                    rule6total = posValues[zeroIndexCount+1][1] - posValues[zeroIndexCount][1];
                    rule6 = rule6total + rule6;
                    rule6total = 0;
                    rule6State=true;
                }
                
                else
                {   
                    errorLog[logCounter]="Illegal subtractive notation found with V, D or L";
                    logCounter++;
                    rule5=0;
                    illegalSubtractive=true;
                }
                }
                
               // Rule 7: When a Roman numeral of a smaller value is placed between two numerals of greater value, 
               //it is subtracted from the numeral on its right. For example, XIV = 10 + (5 - 1) = 14, 
               //or, XIX = 10 + (10 - 1) = 19
                
                System.out.println("Rule 7");
                // rule 7 will not stall by rule 4  since the the right hand side character in rule 4 is always bigger.
                
                if (m!=posValues.length && posValues.length>2) // a numeral can not be in middle if it is the last in the numeral array
                // it also can not be in middle if the first numeral. Hence using m index notation.
                
                {
                    if (processedConversion[m]!=1)
                    {
                    
                    //START FROM HERE AND TEST  XI
                
                if (m!=posValues.length-1)
                {
                    
                    
                if (posValues[m][1]<posValues[m-1][1] && posValues[m][1]<posValues[m+1][1])
                {
                    
                    //System.out.println("what " + (m-1));
                    
                    //processedConversion[m]=1;
                    //System.out.println("This is value marked with 1: " +  posValues[processedConversion[numeralsToString.indexOf(subtractiveNumerals[m])][1]]);
                    //processedConversion[m+1]=1;
                    //System.out.println("This is value marked with 1: " +  posValues[processedConversion[numeralsToString.indexOf(subtractiveNumerals[m+1])][1]]);
                    
                    rule7total = posValues[m+1][1] - posValues[m][1];
                    rule7 = rule7total + rule7;
                    rule7total = 0;
                    
                    System.out.println("total at rule 7:" + rule7);
                    
                    }
                    
                }
                
                }
            }
                zeroIndexCount++;
                temp1++;
                
                System.out.println("This is overall total:" + (total1));
        }
    }
        
        
        // THIS WILL NOW CHECK IF EACH NUMBER HAS OR HAS NOT BEEN PROCESSED IN CHECKING SO far
        
        int counter=0;
        int unTotal=0;
        
        System.out.println("---------------------------------------runningTotal IS: " +  runningTotal);
        
        for (int c: processedConversion)
        {
            System.out.println("This is value of c: " + c);
            System.out.println("This is the numeral value: " + posValues[counter][1]);
            
            if (c!=1)
            {
                unTotal = posValues[counter][1] + unTotal;
                System.out.println("This has not been added to the total" + unTotal);
                System.out.println("This is the position: " + counter);
            }
            
            counter++;
        }
        
            // this is part of rule 3
            
            if (countD>1 || countL>1 || countV>1)
            {
                errorLog[logCounter]="Invalid roman numeral. Numeral V or D or L has occured more than once";
                logCounter++;
                rule3Fail = true;
            }
            
             if (inputtedNumerals.length==2)  // checking number with 2 numerals
        {
            if (posValues[0][0]==posValues[1][0] && rule3Fail==false)  // if values identical straight forward conversion
            {
                twoNumerals = posValues[0][1] + posValues[1][1];
                
                
            System.out.println("Conversion is double:" + (twoNumerals));
            
            //System.out.println("XL: " +  posValues[0][1]);
            processedConversion[0]=1;
            
            processedConversion[1]=1;
            //System.out.println("This is value marked with 1: " +  posValues[1][1]);
            
            
            }
        }
        System.out.println("---------------------------------------runningTotal IS: " +  runningTotal);
        System.out.println("number of Vs: " + countV);
        System.out.println("number of Ls: " + countL);
        System.out.println("number of Ds: " + countD);
        
        System.out.println("rule4: " + rule4);
        System.out.println("rule2: " + rule2);
        System.out.println("rule4Single: " + rule4Single);
        System.out.println("rule5: " + rule5);
        System.out.println("rule6: " + rule6);
        System.out.println("rule7: " + rule7);
        System.out.println("threeinarow:" + threeInRow);
        System.out.println("Untotal:" + unTotal);
        
      if (rule3Fail)
            {
                errorLog[logCounter] = "More than 2 instances of V, L and D:"  +  "   D: " + countD + "   V: " + countV + "   L: " + countL;
                
                
                System.out.println("More than 2 instances of V, L and D:");
                System.out.println("D: " + countD);
                System.out.println("V: " + countV);
                System.out.println("L: " + countL);
                
                
                logCounter++;
                
                
            }
            
             // THIS WILL NOW CHECK IF EACH NUMBER HAS OR HAS NOT BEEN PROCESSED IN CHECKING SO far
        
        
         // it is not picking up MM or II
            // This suggests that they have been marked as processedconversion
            
            
             if (!illegalSubtractive && !IplacedIncorrect && !rule3Fail && !illegalFourInARowState)
        {
            System.out.println(unTotal);
            
            System.out.println("---------------------------------------runningTotal IS: " +  runningTotal);
            
            //not sure why required!
            if (runningTotal>0 && unTotal>0 && flagSet==true)
            {
                runningTotal = runningTotal / 2;
            }
            
            validNumber=true;
            
            System.out.println("---------------------------------------runningTotal IS: " +  runningTotal);
        
        System.out.println("--- Note: It relies on old logic calculations from rule4Single:("+rule4Single+")" +  "splitting runningTotal under circumstances");   
        System.out.println("********************* NEW LOGIC:  GRAND TOTAL*************");
        System.out.println(numeralsToString + " is a VALID roman numeral");
        
        //not sure why this is required!
        if (rule4Single<runningTotal)
        {
            runningTotal=rule4Single;
        }
        
        System.out.println("runningTotal:" + runningTotal);
         System.out.println("Not added total:" + unTotal );
        System.out.println("**TOTAL: "           + (runningTotal+unTotal));
        System.out.println("**********************************");
        
        getLogs();
        
        //System.exit(0);
        
        }
        
        if (!validNumber)
        {
            
        System.out.println("\n\n********************* NEW LOGIC:   GRAND TOTAL*************");
        System.out.println(numeralsToString + " is an INVALID roman numeral");
        System.out.println("Address the issues outputted for a valid Roman numeral");
        
        getLogs();
        
        System.out.println("**********************************");
        }   
        
        System.out.println("\n\n\n********************************************");
            System.out.println("OLD PROGRAMME LOGIC");
            System.out.println("\n********************************************");
            
        System.out.println(numeralsToString + " is the roman numeral");    
        System.out.println("number of Vs: " + countV);
        System.out.println("number of Ls: " + countL);
        System.out.println("number of Ds: " + countD);
        
        
        System.out.println("rule4: " + rule4);
        System.out.println("rule2: " + rule2);
        System.out.println("rule4Single: " + rule4Single);
        System.out.println("rule5: " + rule5);
        System.out.println("rule6: " + rule6);
        System.out.println("rule7: " + rule7);
        System.out.println("threeinarow:" + threeInRow);
        System.out.println("Untotal:" + unTotal);
        
        getLogs();
        
        
      //if (rule3Fail)
            //{
             //   System.out.println("More than 2 instances of V, L and D:");
            //    System.out.println("D: " + countD);
              //  System.out.println("V: " + countV);
             //   System.out.println("L: " + countL);
                //System.exit(0);
            //}
            
        
      if (!illegalSubtractive && !IplacedIncorrect && !illegalThreeInARowState && !rule3Fail)
        {
            
            
             if (unTotal!=0 && rule6!=0 && rule5!=0 && rule4Single!=0)
             {
                 System.out.println("total1: " + (rule4Single + unTotal + rule5));
                 System.exit(0);
             }
             
             
             if (unTotal!=0 && rule6==0 && rule5==0 && threeInRow!=0)
             {
                 System.out.println("total2_temp: " + (rule4Single + unTotal + threeInRow));
                 System.exit(0);
             }
             
            
            if (rule4Single!=0 && unTotal!=0)
            {
                System.out.println("total2: " + (rule4Single + unTotal));
                System.exit(0);
            }
            
             if (rule4Single!=0 && rule2!=0 && rule5!=0)
            {
                System.out.println("total3: " + (rule4Single + rule2 + rule5));
                System.exit(0);
            }
            
             if (rule4Single==0 && rule2!=0 && rule5!=0 && rule4!=0)
            {
                System.out.println("total4: " + (rule4 + rule2 + rule5));
                System.exit(0);
            }
            
            if (unTotal==0 && rule5==0 && rule2==0 && rule4==0 && threeInRow!=0)
            {
                System.out.println("Total5: " + (threeInRow));
            System.exit(0);
            }
            
            if (unTotal!=0 && rule5!=0 && rule6!=0)
            {
            System.out.println("Total6: " + (unTotal+rule6));
            System.exit(0);
            }
            
            
              if (unTotal!=0 && rule5!=0)
            {
            System.out.println("Total6: " + (unTotal+rule5));
            System.exit(0);
            }
            
             if (unTotal!=0 && rule5==0 && rule2==0 && rule4==0 && threeInRow!=0)
            {
                System.out.println("Total7: " + (unTotal+threeInRow));
            System.exit(0);
            }
            
            if (rule2!=0 && rule4Single==0 && rule5==0 && rule6==0 && threeInRow!=0)
            {
                System.out.println("Total8: " + (threeInRow+rule2));
                System.exit(0);
            }
            
            if (rule2!=0 && rule4Single==0 && rule5==0 && rule6==0 && unTotal!=0)
            {
                System.out.println("Total9: " + (rule2+unTotal));
                System.exit(0);
            }
            
            if (rule2!=0 && rule4Single==0 && rule5==0 && rule6==0)
            {
                System.out.println("Total10: " + rule2);
                System.exit(0);
            }
            
            if (rule4Single!=0 && rule5!=0 && rule6!=0)
            {
                System.out.println("Total11: " + (rule4Single+rule5));
                System.exit(0);
            }
        
        if (unTotal!=0 && rule5==0 && rule2==0 && rule4==0 && rule4Single==0)
            {
                System.out.println("Total12: " + (unTotal));
            System.exit(0);
            }
            
        if (unTotal!=0 && rule4!=0)
            {
            System.out.println("Total13: " + (unTotal + rule4));
            System.exit(0);
            }
        
         if (rule5!=0 && rule6!=0 && unTotal!=0)
            {
                System.out.println(numeralsToString + "is an invalid roman numeral");
                System.out.println(numeralsToString + "INVESTIGATE CODE OR CONDITIONAL IF LOOPS FOR TOTAL IF ENTER OCCURS");
                
                System.exit(0);
            }
            
         if (rule6!=0 && rule5!=0)
            {
            System.out.println("Total14: " + (rule6+rule5));
            System.exit(0);
            }
        
        
         
        if (rule6!=0 && rule2!=0)
        {
            System.out.println("Total 15: " + (rule6+rule2));
            System.exit(0);
        }
        
           if (rule5!=0 && rule2!=0 && threeInRow==0 && rule4Single!=0)
        {
            System.out.println("Total16: " + (rule5+rule2));
            System.exit(0);
        }
        
        
        if (rule5!=0 && rule2!=0 && threeInRow!=0)
        {
            System.out.println("Total17: " + (rule5+rule2));
            System.exit(0);
        }
        
       
         if (rule5!=0 && rule2!=0)
        {
            System.out.println("Total18: " + (rule5+rule2));
            System.exit(0);
        }
        
        
        
        
        if (rule5!=0 && unTotal!=0)
            {
                System.out.println("Total19: " + (rule5+unTotal));
                System.exit(0);
            }
            
               if (rule5!=0 && rule4!=0)
            {
                System.out.println("Total20: " + (rule5 + rule4));
                System.exit(0);
            }
            
            if (rule5!=0 && threeInRow!=0 && rule4Single!=0)
            {
                System.out.println("Total21: " + (rule5+threeInRow));
                System.exit(0);
            }
            
            
            if (rule5!=0 && threeInRow!=0)
            {
                System.out.println("Total22: " + (rule5+threeInRow));
                System.exit(0);
            }
        
            if (rule5!=0)
            {
                System.out.println("Total23: " + (rule5));
                System.exit(0);
            }
            
            if (rule2!=0 && unTotal!=0 && rule4Single!=0)
            {
                System.out.println("Total24: " + (rule2 + unTotal + rule4Single));
                System.exit(0);
                
            }
            
            
            if (rule2!=0 && rule4Single!=0)
            {
                System.out.println("Total25: " + (rule2 + rule4Single));
                System.exit(0);
            }
        
           if (rule2!=0)
            {
                System.out.println("Total26: " + (twoNumerals));
                System.exit(0);
            }
        
         
            if (rule4Single!=0)
            {
                System.out.println("Total27 is: " + (unTotal + rule4Single));
                System.exit(0);
            }
            
               if (rule4!=0)
            {
                System.out.println("Total28 is: " + (rule4));
                System.exit(0);
            }
            
        }
            System.out.println(numeralsToString +  " is NOT a VALID roman numeral");
            System.out.println("Address the issues outputted for a valid Roman numeral");
            
        
        //System.out.println("this is the overall sum: " + (rule2+rule4Single+rule5+rule6+rule7));
       // System.out.println("this is the overall sum: " + (rule5 + rule2));
        
        // This is the best logic found on the internet for roman numeral conversion.
        // Logic will be coded one by one and see if it works overall
    /*
    
    It is necessary for us to remember the rules for reading and writing Roman numbers in order to avoid mistakes. Here is a list of the basic rules for Roman numerals.

    Rule 1: When certain numerals are repeated, the number represented by them is their sum. For example, II = 1 + 1 = 2, or XX = 10 + 10 = 20, or, XXX = 10 + 10 + 10 = 30.
    Rule 2: It is to be noted that no Roman numerals can come together more than 3 times. For example, we cannot write 40 as XXXX
    Rule 3: The xcletters V, L, and D are not repeated.
    Rule 4: Only I, X, and C can be used as subtractive numerals. There can be 6 combinations when we subtract. These are IV = 5 - 1 = 4; IX = 10 - 1 = 9; XL = 50 - 10 = 40; XC = 100 - 10 = 90; CD = 500 - 100 = 400; and CM = 1000 - 100 = 900
    Rule 5: When a Roman numeral is placed after another Roman numeral of greater value, the result is the sum of the numerals. For example, VIII = 5 + 1 + 1 + 1 = 8, or, XV = 10 + 5 = 15,
    Rule 6: When a Roman numeral is placed before another Roman numeral of greater value, the result is the difference between the numerals. For example, IV = 5 - 1 = 4, or, XL = 50 - 10 = 40, or XC = 100 - 10 = 90
    Rule 7: When a Roman numeral of a smaller value is placed between two numerals of greater value, it is subtracted from the numeral on its right. For example, XIV = 10 + (5 - 1) = 14, or, XIX = 10 + (10 - 1) = 19
    Rule 8: To multiply a number by a factor of 1000 a bar is placed over it.
    Rule 9: Roman numerals do not follow any place value system.
    Rule 10: There is no Roman numeral for zero (0).
*/
        
    }
   
}
