0

Basically the User needs to input a Hexadecimal number which then converts it to decimal and binary numbers. I created two separate classes for the decimal and binary conversion and they work perfectly. What I wanted to do is to limit the input number making "90" the minimum input and "FF" the maximum one.

public static void main(String[] args) {
    ForwardorBack Binary = new ForwardorBack();
    Speed Decimal = new Speed();
    String Hexadecimal = "";
    Scanner input = new Scanner(System.in);
    System.out.println("Enter Hexadecimal Number");
    Hexadecimal = input.nextLine();
    while (Hexadecimal.length() > 2 || Hexadecimal.length() < 2) {
        System.out.println("Error Enter different number:");
        Hexadecimal = input.nextLine();
    }
    Decimal.wheels(Hexadecimal);
    Binary.move(Hexadecimal);
}
3
  • 3
    When posting on here, always use Java naming conventions. Variables are in camelCase; PascalCase is reserved for classes. Commented Mar 17, 2018 at 21:47
  • 1
    Use != 2 rather than both < 2 and > 2. Why not convert the input to an int to validate input and then easily compare if it is between 90 and 255 Commented Mar 17, 2018 at 22:27
  • there is no such thing, like number in decimal / hex / octal form. These are only formats on input/output, numeric sense is the same Commented Mar 17, 2018 at 22:48

1 Answer 1

0

Here is a method to use in the head of the while loop

public static boolean validateInput(String input) {
  if  (input.length != 2) {
       return false;
  }

  //Replace with own conversion if needed
  try {
    int decimal = Integer.parseInt(hex, 16);
    if (decimal >= 90 && decimal <= 255) {
        return true;
    }
  } catch (NumberFormatException e) {
    return false;
  }
}

Then use it like so

while(!validateInput(hexadecimal)) {
  System.out.println("Error Enter different number:");
  hexadecimal = input.nextLine();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.