how to perform a mathematical operation in the same input, using javaserver faces?

1

Code xhtml

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:head>
  <title></title>
</h:head>
<h:body>
  <p:layoutUnit position="center">
    <h:form>
      <h:panelGrid columns="3" cellpadding="5">
        <h:outputText value="Numero:" />
        <p:inputText id="surname" value="#{operacion.numero}">
          <p:ajax event="blur" update="out2" listener="#{operacion.resolver()}" />
        </p:inputText>
        <h:outputText id="out2" value="#{operacion.respuestaEntera}" />
      </h:panelGrid>
    </h:form>
  </p:layoutUnit>
  <br />
</h:body>

</html>

Managed bean that allows you to call the function in the xhtml

public class Operacion {

    private String numero;
    private String respuesta;
    private int respuestaEntera;

    public Operacion() {
    }

    public int getRespuestaEntera() {
        return respuestaEntera;
    }

    public void setRespuestaEntera(int respuestaEntera) {
        this.respuestaEntera = respuestaEntera;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public String getRespuesta() {
        return respuesta;
    }

    public void setRespuesta(String respuesta) {
        this.respuesta = respuesta;
    }

    public void resuelveOperacion() {
        String num1 = "";
        String num2 = "";
        int numero2 = Integer.parseInt(this.numero);
        for (int x = 0; x < numero.length(); x++) {
            if (numero.charAt(1) == '+') {
                if (numero == null) {
                    num1 = numero;
                } else {
                    num2 = numero;
                }
            }
            int suma = Integer.parseInt(num1) + Integer.parseInt(num2);
            respuestaEntera = suma;
        }
    }
}
    
asked by Anthoni Hernandez 13.12.2018 в 17:33
source

1 answer

0

In principle, your view is correct, it will show and collect the information exactly as you have planned it.

Just remember that it is advisable in primefaces to use the p-label whenever you want to use a component of primefaces example:

<p:outputLabel for="@next" value="Numero:" /> <!-- @next o surname lo que prefieras-->
<p:inputText id="surname" value... /> <!-- surname supongo que sean numero -->

But the above is not a problem, your view code is correct.

The problem you have in resolvesOperation ()

The question is not entirely clear that you intend to do in this method, but it will not work.

1 int numero2 = Integer.parseInt(this.numero);
2 for (int x = 0; x < numero.length(); x++) {

In the line of code 1 you treat the number as a string to which you transform to whole, perfect if you write "3" - > 3.

On line 2 you will go through the String number to look for a "+" - > INCORRECT, either it's a number or it's an operation, it can not be both.

Assuming that an operation is what you expect in the variable number, you would have the following - > BASIC CASE for 2 + 3 for example

public void resuelveOperacion() {
int num1 = 0;
int num2 = 0;
String regex = "+"; //Dale un par de vueltas a tu expresión regular

// analyzing the string y suponiendo que se ha entrado "2 + 3" de lo contrario 
// para "2 + " daría un error de arrayoutofboundexception
String[] numeros = this.numero.split(regex);
if (numero.contains("+")) {
    num1= Integer.parseInt(numeros[0]);
    num2= Integer.parseInt(numeros[1]);
    return num1 + num2;
}

}

Now you have to give it a couple of laps to make it generic and be able to use operations of type "3 * 2 + 1" or more advanced 1 + (3 * 2).

Clue: In your regular expression " regex ", you can search by numbers and operators so that the array of " String[] numeros " is [3, *, 2, +, 1] and you just have to go through it looking for operators and doing this operations between the elements [x-1] op [x + 1] verifying that your formula is correct [x + 1] always exists "3+" for the position x = 1 - > x [1] = + [x-1] = 3 [x + 1] = ERROR. You have to capture it or not allow this type of situations "3+", "+3", "++ 2" ?, "2 ++"? ..

I hope you find it useful.

    
answered by 18.12.2018 в 10:08