简体   繁体   English

使用 Java 和 HTML 获取两个字符串输入总和的正确方法是什么?

[英]What is the correct way to get the sum of two string inputs using Java with HTML?

I am attempting to make an HTML page where the user inputs 2 numbers as strings, clicks submit, and the sum is displayed.我正在尝试制作一个 HTML 页面,其中用户输入 2 个数字作为字符串,点击提交,然后显示总和。 I screwed up something along the way while parsing and I am not sure how to correct it.我在解析过程中搞砸了一些东西,我不知道如何纠正它。 How do I fix this issue?我该如何解决这个问题?

java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
    java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
    java.base/java.lang.Float.parseFloat(Float.java:461)
    basicaddition.BasicAddition.processRequest(BasicAddition.java:42)

The code:编码:

    String EmptyError = "";
    String FirstNumber = request.getParameter("FirstNumber");
    String SecondNumber = request.getParameter("SecondNumber");

    float firstNumber = Float.parseFloat(FirstNumber);
    float secondNumber = Float.parseFloat(SecondNumber);

    if(FirstNumber.isBlank() && SecondNumber.isBlank())
    {
      EmptyError = "There is no sum";
    }

   else {
            try {
                 firstNumber = Float.parseFloat(FirstNumber);

            } catch (NumberFormatException ex) {
                EmptyError = "Value is not a number";
            }

            try {
                secondNumber = Float.parseFloat(SecondNumber);

            } catch (NumberFormatException ex) {
                EmptyError = "Value is not a number";
            }


        }

      Float Result = firstNumber + secondNumber;

    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet BasicAddition</title>");            
        out.println("</head>");
        out.println("<body>");

        out.println("<h1>Returns the sum of two numbers, ");
        out.println("</h1>");

        out.println("<input type='text' name='FirstNumber' />");
        out.println(EmptyError);

        out.println("<input type='text' name='SecondNumber' />");
        out.println(EmptyError);

        out.println("<input type='submit' value='Get the sum' />");
        out.println(EmptyError);
        out.println(Result);


        out.println("<h1>Servlet BasicAddition at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");

I'm seeing you're trying to parse the numbers before the condition!我看到您正在尝试在条件之前解析数字!

float firstNumber = Float.parseFloat(FirstNumber);
float secondNumber = Float.parseFloat(SecondNumber);

Those two lines throw an exception because FirstNumber and SecondNumber are not a valid number!这两行抛出异常,因为 FirstNumber 和 SecondNumber 不是有效数字!

In your if condition, you have to use 'OR' operator.在您的if条件下,您必须使用“OR”运算符。 Otherwise at least one condition is false it jumps to else .否则至少有一个条件为false它跳转到else you have to remove String to float conversion before the if statement.您必须在if语句之前删除String以进行float转换。 Check below code:检查以下代码:

    String EmptyError = "";

    String FirstNumber = request.getParameter("FirstNumber");
    String SecondNumber = request.getParameter("SecondNumber");

    float firstNumber, secondNumber;

    if (FirstNumber.isBlank() || SecondNumber.isBlank()) {
        EmptyError = "There is no sum";
    } else {
        //Rest of the code
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM