0

I am writing a program that translates from Roman numerals to decimal numbers.

For some reason it does not return the value when it checks the user's input. However it already fixed,

what I'm facing right now is: The code does not respond me the number (it's keep show a blank screen after the input).

How can I fix this? Is there an issue in my code? I am just a starter so what I have learned is just basic stuff.

public static void main(String[] args) {
        // Fill in the body
        Scanner in= new Scanner(System.in);
        String user = promptUserForNumeral(in);
        while (user.length()!=0) {
            int numb= convertNumeralToNumber(user);
            System.out.println("The numeral "+user+ " is the decimal number "+numb);
            user = promptUserForNumeral(in);
        }
    }
private static String promptUserForNumeral(Scanner inScanner) {
    // Fill in the body
    System.out.println("Enter a roman numeral (Q to quit): ");
    String i = inScanner.nextLine();
    while (i.length()<=0) {
        System.out.println("ERROR! You must enter a non-empty line!");
        System.out.println("Enter a roman numeral (Q to quit): ");
        i = inScanner.nextLine();
    }
    if ( i.equalsIgnoreCase("q")) {
        System.out.println("Goodbye!");
        System.exit(0);
    }
    return i;

}
private static int convertNumeralToNumber(String numeral) {
    // Fill in the body
    int numb = 0;
    int n=0;
    int ch=0;
    while (n<numeral.length()) {
        char l= numeral.charAt(n);
        numb=convertCharacterToNumber(l);
        if (numb<0) {
            System.out.println("Cannot be define");
            n++;
        }
        else if (n==numeral.length()) {
            ch+=numb;
        }
        else {
            int nnumb=convertCharacterToNumber(numeral.charAt(n));
            if (nnumb>numb) {
                ch+=nnumb-numb;
                n++;                    
            }
            else {
                ch+=numb;
            }
        }
    }
    if (ch>3999) {
        System.out.println("Input number must be less than 3999");
        numb=ch;
    }
    return numb;


}


private static int convertCharacterToNumber(char numeral) {
    // Fill in the body
    int n=0;
    if (numeral=='m' || numeral =='M') {
        return 1000;
    }
    else if (numeral=='d' || numeral=='D') {
        return 500; 
    }
    else if (numeral=='c' || numeral=='C') {
        return 100;
    }
    else if (numeral=='l' || numeral=='L') {
        return 50;
    }
    else if (numeral=='x' || numeral=='X') {
        return 10;
    }
    else if (numeral=='v' || numeral=='V') {
        return 5;
    }
    else if (numeral=='i' || numeral=='I') {
        return 1;
    }
    else {
        return -1;
    }

}       

}

bscouth
  • 9
  • 2
  • 8
  • 1
    What specifically doesn't return a value? You can't expect anyone to pour through a wall of code without a hint of where to start looking for the problem. Could you please further explain what isn't working? – Michael Zajac Nov 04 '14 at 04:02
  • when user input the Roman number, it will check if it legit or not, which in the first method. so if it legit it will return what user input – bscouth Nov 04 '14 at 04:23

4 Answers4

0

Check this

while (i.length()>=0) {
    if (i.length()==0) {
        System.out.println("ERROR! You must enter a non-empty line!");
        System.out.println("Enter a roman numeral (Q to quit): ");
        i = inScanner.nextLine();
    }
    else if ( i.equalsIgnoreCase("q")) {
        System.out.println("Goodbye!");
        System.exit(0);
    }
}
return i;

This won't quit or return anything while i.length() > 0. That return is dead code if user didn't enterq. Solution: Specify a else with break; then it will work.

else
   break;
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

I would rewrite your while loop :

while (i.length()<=0) {
    System.out.println("ERROR! You must enter a non-empty line!");
    System.out.println("Enter a roman numeral (Q to quit): ");
    i = inScanner.nextLine();
}
if ( i.equalsIgnoreCase("q")) {
    System.out.println("Goodbye!");
    System.exit(0);
}
return i;
Eran
  • 387,369
  • 54
  • 702
  • 768
0

You have lots of redundant conditions. The problem lies within this loop:

    while (i.length() >= 0) {
        if (i.length() == 0) {
            System.out.println("ERROR! You must enter a non-empty line!");
            System.out.println("Enter a roman numeral (Q to quit): ");
            i = inScanner.nextLine();
        } else if (i.equalsIgnoreCase("q")) {
            System.out.println("Goodbye!");
            System.exit(0);
        }
    }

Take any value for i like "V".

  • It's length is greater than zero, hence it enters the loop.
  • It's length is again not zero in the first if condition, hence it proceeds to the elseIf
  • Since the value isn't a "q", the else part doesn't execute either.
  • So it goes back to the loop's start & again checks the condition if length is greater than zero.

So, you have an infinite loop. Work through your logic again & remove any unnecessary conditions. You can also use the break; statement to terminate the loop.

Vineet
  • 897
  • 10
  • 27
0
public class stringTest {
public static void main(String[] args) {
    // Fill in the body
    Scanner in= new Scanner(System.in);
    String user = promptUserForNumeral(in);
    while (user.length()!=0) {
        int numb= convertNumeralToNumber(user);
        System.out.println("The numeral "+user+ " is the decimal number "+numb);
        user = promptUserForNumeral(in);
    }
}
private static String promptUserForNumeral(Scanner inScanner) {
    // Fill in the body
    System.out.println("Enter a roman numeral (Q to quit): ");
    String i = inScanner.nextLine();
    while (i.length()>=0) {
        if (i.length()==0) {
            System.out.println("ERROR! You must enter a non-empty line!");
            System.out.println("Enter a roman numeral (Q to quit): ");
            i = inScanner.nextLine();
        }
        else if ( i.equalsIgnoreCase("q")) {
            System.out.println("Goodbye!");
            System.exit(0);
        }
        else return i; // in your program the while is never ending, so it does not return any value.
    }
    return "";
}
private static int convertNumeralToNumber(String numeral) {
    // Fill in the body
    int preNumber = 0;
    int curNumber = 0;
    int n=0;
    int ch=0;
    while (n<numeral.length()) {
        char l= numeral.charAt(n);
        curNumber=convertCharacterToNumber(l);
        if (curNumber<0) {
            System.out.println("Cannot be define");
            System.exit(0);
        }
        else {
            // I have changed the logic to evaluated decimal Number equivalent to Roman Literal
            if(preNumber < curNumber && n != 0) ch = curNumber - ch;
            else ch += curNumber;
            preNumber = curNumber;
        }
        n++;
    }
    return ch;
}


private static int convertCharacterToNumber(char numeral) {
    // Fill in the body
    if (numeral=='m' || numeral =='M') {
        return 1000;
    }
    else if (numeral=='d' || numeral=='D') {
        return 500; 
    }
    else if (numeral=='c' || numeral=='C') {
        return 100;
    }
    else if (numeral=='l' || numeral=='L') {
        return 50;
    }
    else if (numeral=='x' || numeral=='X') {
        return 10;
    }
    else if (numeral=='v' || numeral=='V') {
        return 5;
    }
    else if (numeral=='i' || numeral=='I') {
        return 1;
    }
    else {
        return -1;
    }

}    
}

You can probably look into promptUserForNumeral method, I think it is not necessary. You can include that in the main while loop to look for user errors.

saikumarm
  • 1,565
  • 1
  • 15
  • 30