Home > Mobile >  Java - how to validate following input format 'operand operator operand' (e.g. 1.4 * 5)
Java - how to validate following input format 'operand operator operand' (e.g. 1.4 * 5)

Time:11-28

I'm breaking my head trying to figure out what is wrong in my code. I'm asking the user to insert the following format:

LeftOperand operator RightOperand

Where:

  • LeftOperand - can be any int or float number
  • operator - can be , -, * or /
  • RightOperand - can be any int or float number

I searched and found few regular expressions but they all doesn't seems to work for me, as anything besides the following: 3 /-/*// 5 returns an error. Should be valid:

  • 1 3
  • 1 / 4
  • 1.3 - 4
  • 4 - 5.3
  • 123 * 3434
  • 12.34 * 485

but in fact only those are valid while the rest return error:

  • 1 3 - correct
  • 1 / 4 - correct
  • 1.3 - 4 - error
  • 4 - 5.3 - error
  • 123 * 3434 - error
  • 12.34 * 485 - error

I'm currently using the following regex in my code:

"[((\\d \\.?\\d*)|(\\.\\d ))] [ ,\\-,*,/] [((\\d \\.?\\d*)|(\\.\\d ))]"

Tried all kind of regex, but none seems to work, and I just don't know what am I doing wrong:

[[ -]?([0-9]*[.])?[0-9]] [ ,\-,*,/] [[ -]?([0-9]*[.])?[0-9]]

This is my client code:

private static final int SERVER_PORT = 8080;
private static final String SERVER_IP = "localhost";
private static final Scanner sc = new Scanner(System.in);
private static final String regex = "[((\\d \\.?\\d*)|(\\.\\d ))] [ ,\\-,*,/] [((\\d \\.?\\d*)|(\\.\\d ))]";

public static void main(String[] args) throws IOException {
    // Step 1: Open the socket connection
    Socket serverSocket = new Socket(SERVER_IP, SERVER_PORT);

    // Step 2: Communication-get the input and output stream
    DataInputStream dis = new DataInputStream(serverSocket.getInputStream());
    DataOutputStream dos = new DataOutputStream(serverSocket.getOutputStream());

    //Run until Client decide to disconnect using the exit phrase
    while (true) {
        // Step 3: Enter the equation in the form -
        // "operand1 operation operand2"
        System.out.print("Enter the equation in the form: ");
        System.out.println("'operand operator operand'");

        String input = sc.nextLine();
        // Step 4: Checking the condition to stop the client
        if (input.equals("exit"))
            break;

        //Step 4: Check the validity of the input and
        // return error message when needed
        if (input.matches(regex)) {
            System.out.println("Expression is correct: "   input);
        } else {
            System.out.println("Expression is incorrect, please try again: ");
            continue;
        }

        // Step 5: send the equation to server
        dos.writeUTF(input);

        // Step 6: wait till request is processed and sent back to client
        String ans = dis.readUTF();
        // Step 7: print the response to the console
        System.out.println("Answer="   Double.parseDouble(ans));
    }
}

CodePudding user response:

private static final String NUM = "(-?\\d (\\.\\d*)?)";
private static final String OP = "([- */])"; // Minus at begin to avoid confusion with range like a-z.
private static final String WHITESPACE = "\\s*";
private static final String regex = NUM   WHITESPACE   OP   WHITESPACE   NUM;

Note:

  • $1 corresponds with the first number
  • $3 corresponds with the operator
  • $4 corresponds with the second number
  • (...) is a group ($1, $2, ...; $0 is all)
  • [...] is a character set, one char match
  • X? X optional
  • X* X 0 or more times
  • X X 1 or more times
  • \\s space, tab
  • \\d digit
  • Related