2

I'm working on a calculator program that has 4 modes.
Binary(base 2), octal(base 8), decimal(base 10), and hexadecimal(base 16).

I want to know how I can check if the user input that is entered matches the current mode. For example if the calculator is set to octal mode how do I check if what the user entered is in base 8? Or if it's in hexadecimal mode how to check if it's base 16?

The program starts in decimal mode and displays this menu.

private static String displayMenu(String mode) {
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    System.out.println(mode + " mode\n");
    System.out.println("Bin - Binary               +");
    System.out.println("Oct - Octal                -");
    System.out.println("Dcm - Decimal              *");
    System.out.println("Hex - Hexadecimal          /");
    System.out.println("Q   - Quit                 =");
    System.out.print("Option or value: ");
    String option = input.nextLine();
    option.toUpperCase();
    return option;
}

The user can either change the mode or enter a value for the current mode.

Kirisaki Z
  • 25
  • 5

4 Answers4

1

For octal, typically octal numbers are represented with a preceding zero, for example 012. Hexadecimal numbers are typically preceded with 0x, for example 0xFF.

You could read in the user's input as a String, then look at the first few chars to see if the number starts with a 0 (but isn't just a 0) or starts with "0x".

You'll have to do something different with binary, as you can't really tell if 1001 is binary or decimal.

Apitosu
  • 86
  • 5
1

In general, there are two ways to deal with user input: 1) Restrict what the user is allowed to provide as input or 2) validate input and display an error message when the input is invalid. For this particular program, I suggest either of the following:

  1. If this is a GUI program with buttons for each number (like on a physical calculator), you can just change the available buttons as soon as the user selects the mode. For example, when the user selects octal mode, only provide buttons for the digits 0 through 8. This automatically disallows the user to type a 9 or any letter.

  2. If the program is text-based or you allow the user, to type numbers from the keyboard, you will need to parse the input. One way to do this is with Integer.parseInt(). One version of this function allows you to specify the base, which will be determined by the mode which the user selects. It will throw an exception when the input string contains invalid characters. You can catch the exception and display an appropriate error message.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

A certain number can match many bases so you cannot know for sure what base the number has been entered in. If the user inputs the number in an established format, you can use @Apitosu's answer to check its base.

If you just want to check if the entered number is valid for a certain base, you can use Long.parseLong's overload which receives a radix as a second parameter and will raise an exception if the number is invalid for the selected radix.

Source.

Community
  • 1
  • 1
Roberto Linares
  • 2,215
  • 3
  • 23
  • 35
0

I suggest you take the number and convert it string (probably no choice for hex values) and then just run them though a regular expression to see if it passes. Something like:

[0-1]+ //Binary
[0-7]+ //Octal
[0-9]+ //Decimal
[0-9A-F]+ //Hexadecimal

I'm sure these can be improved on. Note, there are scenarios where a number will match multiple expressions.

Sasang
  • 1,261
  • 9
  • 10