I'ma noob and need some help from the pros.
For school I gotta make a program to convert numbers into roman numerals 1-10 and return an error if its not 1-10.
Got it all pretty much figured out but I want to know how to have it where if someone enters the wrong number, it returns the error then will restart the program from the start again so they can enter the right number. Maybe even a dialogue box at the end asking if they would like to retry or not.
Any advice or help would be greatly appreciated!
Thanks!

Heres what i got so far

import javax.swing.JOptionPane;  // Needed for JOptionPane

/**
  Chapter 3.1 Roman Numerals
*/

public class Chapter3dot1
{
   public static void main(String[] args)
   {
      int num;
      String input;

      input = JOptionPane.showInputDialog("Enter a number between 1 and 10 " +
                     "and I will convert it to a Roman numeral: ");
      num = Integer.parseInt(input);

      if (num == 1)
         JOptionPane.showMessageDialog(null, "I");

      else if (num == 2)
         JOptionPane.showMessageDialog(null, "II");

      else if (num == 3)
         JOptionPane.showMessageDialog(null, "III");

      else if (num == 4)
         JOptionPane.showMessageDialog(null, "IV");

      else if (num == 5)
         JOptionPane.showMessageDialog(null, "V");

        else if (num == 6)
         JOptionPane.showMessageDialog(null, "VI");

        else if (num == 7)
         JOptionPane.showMessageDialog(null, "VII");

      else if (num == 8)
         JOptionPane.showMessageDialog(null, "VIII");

      else if (num == 9)
         JOptionPane.showMessageDialog(null, "IX");

      else if (num == 10)
            JOptionPane.showMessageDialog(null, "X");

      else           //error message   
         JOptionPane.showMessageDialog(null, "Not good at following instructions?" +
                                                "\nI said enter a number between 1 and 10!");
        System.exit(0);

   }
}

Recommended Answers

All 9 Replies

What about a while loop? Something like this:

  boolean shouldContinue = true;
  while(shouldContinue) {
    shouldContinue = false;
    input = JOptionPane.showInputDialog("Enter a number between 1 and 10 " +
                   "and I will convert it to a Roman numeral: ");
    num = Integer.parseInt(input);
    if (num == 1)
       JOptionPane.showMessageDialog(null, "I");
    else if (num == 2)
       JOptionPane.showMessageDialog(null, "II");
    else if (num == 3)
       JOptionPane.showMessageDialog(null, "III");
    else if (num == 4)
       JOptionPane.showMessageDialog(null, "IV");
    else if (num == 5)
      JOptionPane.showMessageDialog(null, "V");
    else if (num == 6)
      JOptionPane.showMessageDialog(null, "VI");
    else if (num == 7)
      JOptionPane.showMessageDialog(null, "VII");
    else if (num == 8)
      JOptionPane.showMessageDialog(null, "VIII");
    else if (num == 9)
      JOptionPane.showMessageDialog(null, "IX");
    else if (num == 10)
      JOptionPane.showMessageDialog(null, "X");
    else {          //error message   
      JOptionPane.showMessageDialog(null, "Not good at following instructions?" +
                                            "\nI said enter a number between 1 and 10!");
      if( JOptionPane.showConfirmDialog(null,
                 "Do you want to try again?",
                 "Try again?", JOptionPane.YES_NO_OPTION) 
                  == JOptionPane.YES_OPTION )
        shouldContinue = true;
    };
  };
  System.exit(0);

Or, you can do it slightly differently if you want to allow retries even if the person entered a good number. You get the basic idea here: use a while-loop and set the flag depending on what happened in the loop, to trigger a repeat or not.

Good suggestion Mike! I'd just make one alteration in your showMessageDialog() text, to

      JOptionPane.showMessageDialog(null, "Not good at following instructions?" +
                                            "\nI said enter a number between 1 and 10 bonehead!");

:-)

thanks for the help. for some reason it would not work.
also isn't the System.exit(0); supposed to be the last line??
i just figured out my next chapter is all about while loop thing so hopefully i will learn it
thanks for the help

@rubberman:
LOL!
I guess it takes an experience software engineer like you to make such a necessary addition. How many times does the word "bonehead" appear in your 10M lines of 6-sigma production code? I guess with 1 chance in a million to fail, you can write anything you like in the error messages.

Btw, the original message-box text was from the OP, not me.

In case u need to loop even if the user enter correct number take ur if condition one place down....

import javax.swing.JOptionPane; // Needed for JOptionPane
/**
    Chapter 3.1 Roman Numerals
 */
public class Chapter3dot1
{
    public static void main(String[] args)
    {
        int num;
        String input;
        boolean shouldContinue = true;
        while(shouldContinue) 
        {
            shouldContinue = false;
            input = JOptionPane.showInputDialog("Enter a number between 1 and 10 " +
            "and I will convert it to a Roman numeral: ");
            num = Integer.parseInt(input);
            if (num == 1)
                JOptionPane.showMessageDialog(null, "I");
            else if (num == 2)
                JOptionPane.showMessageDialog(null, "II");
            else if (num == 3)
                JOptionPane.showMessageDialog(null, "III");
            else if (num == 4)
                JOptionPane.showMessageDialog(null, "IV");
            else if (num == 5)
                JOptionPane.showMessageDialog(null, "V");
            else if (num == 6)
                JOptionPane.showMessageDialog(null, "VI");
            else if (num == 7)
                JOptionPane.showMessageDialog(null, "VII");
            else if (num == 8)
                JOptionPane.showMessageDialog(null, "VIII");
            else if (num == 9)
                JOptionPane.showMessageDialog(null, "IX");
            else if (num == 10)
                JOptionPane.showMessageDialog(null, "X");
            else { //error message
                JOptionPane.showMessageDialog(null, "Not good at following instructions?" +
                "\nI said enter a number between 1 and 10!");

            }
            if( JOptionPane.showConfirmDialog(null,
                    "Do you want to try again?",
                    "Try again?", JOptionPane.YES_NO_OPTION)
                    == JOptionPane.YES_OPTION )
                shouldContinue = true;
        };
        System.exit(0);
    }
}

Thankyou poojavb that works perfectly!
every time i tried to run the one from mike it gave me errors.
This works perfect!!!

Ohh gr8....Your most welcome....
Please do visit again in case of queries or helping others.....

This is quite a bit "ex-post facto", but I have one final suggestion, and that is to use a switch statement instead of the long list of if/else statements. IE, instead of this:

            if (num == 1)
                JOptionPane.showMessageDialog(null, "I");
            else if (num == 2)
                JOptionPane.showMessageDialog(null, "II");
            else if (num == 3)
                JOptionPane.showMessageDialog(null, "III");
            else if (num == 4)
                JOptionPane.showMessageDialog(null, "IV");
            else if (num == 5)
                JOptionPane.showMessageDialog(null, "V");
            else if (num == 6)
                JOptionPane.showMessageDialog(null, "VI");
            else if (num == 7)
                JOptionPane.showMessageDialog(null, "VII");
            else if (num == 8)
                JOptionPane.showMessageDialog(null, "VIII");
            else if (num == 9)
                JOptionPane.showMessageDialog(null, "IX");
            else if (num == 10)
                JOptionPane.showMessageDialog(null, "X");
            else { //error message
                JOptionPane.showMessageDialog(null, "Not good at following instructions?" +
                "\nI said enter a number between 1 and 10!");

use this:

            switch(num)
            {
                case 1:
                JOptionPane.showMessageDialog(null, "I");
                break;
                case 2:
                JOptionPane.showMessageDialog(null, "II");
                break;
                .
                .
                .
                default:
                JOptionPane.showMessageDialog(null, "Not good at following instructions?" +
                "\nI said enter a number between 1 and 10!");
                break;
            }

The compiler will likely do a better job of optimizing this since it doesn't have to evaluate each if/elseif statement.

If you want to play that game...

static final String[] romans = {"I", "II", ... etc
...
if (num>=1 && num< 10) 
    JOptionPane.showMessageDialog(null, romans[i-1]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.