1

I am attempting to build a world clock, to display GMT, PST etc.

The only problem I'm having is that when the user types nothing in the text field, it's not going into the if statement that only executes when the user doesn't enter anything and displays the local time. I have provided all my code to see what the problem is.

html code:

<body>
    <form action="WorldClockWebApp" method="post">
        <table>
            <tr>
                <td>Enter Time Zone:</td>
                <td><input type="text" name="timezone"  /></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
            </tr>       
            <tr>
                <td><input type="submit" value="Submit"  /></td>
            </tr>
        </table>
    </form>
</body>

servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String input = request.getParameter("timezone");
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
    ZoneId z;
    boolean valid = true;
    TimeZone t = null;
    String[] ids = TimeZone.getAvailableIDs();
    if (input != "") // If the user enters in a timezone.
    {
        for (String id : ids) {
            if (input.equalsIgnoreCase(id))
                t = TimeZone.getTimeZone(id);
        }
    }
    if (t == null)
        response.getWriter().append("<h1>What's the time Mr Wolf?</h1>")

                .append("<p>No time provided, but the local time is " + t.getDisplayName().toString() + " is:</p>")
                .append("<h2>" + LocalTime.now(t.toZoneId()).format(dtf) + "</h2>")
                .append("<form action=\"/project4/WorldClock.html\">" + "<button type=\"submit\">Go Back</button>"
                        + "</form>"); // Button gives user option to go back.
    else
        response.getWriter().append("<h1>What's the time Mr Wolf?</h1>")
                .append("<p>The current time in " + t.getDisplayName().toString() + " is:</p>")
                .append("<h2>" + LocalTime.now(t.toZoneId()).format(dtf) + "</h2>")
                .append("<form action=\"/project4/WorldClock.html\">" + "<button type=\"submit\">Go Back</button>"
                        + "</form>"); // Button gives user option to go back.
}
glw
  • 1,646
  • 1
  • 16
  • 20
  • What do you expect when user types nothing? – glw Mar 16 '19 at 00:20
  • when the user types nothing the if(t == null) statement to execute to display no timezone provided – Xander Powel Mar 16 '19 at 00:25
  • why don't you print out `t` right before the `if` and see what it contains? – RisingSun Mar 16 '19 at 06:23
  • do you have `doPost` implemented as well? you seem to be sending the form using `POST` yet you are showing us code for `GET`. https://www.codejava.net/java-ee/servlet/handling-html-form-data-with-java-servlet – RisingSun Mar 16 '19 at 06:28
  • You are comparing strings with `!=`. **Never do that**, always use `equals`. See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MC Emperor Mar 16 '19 at 06:52

2 Answers2

0

I think you need to do if input != null instead of if input !="".

aestheticnoodle
  • 151
  • 4
  • 12
0

assuming that you implement doPost method and it is like doGet method, you should use equals instead of != "" . your code can be like below code.

if (!input.equals(""))