Convert typed-in Text to lowercase

谁都会走 提交于 2019-12-02 05:30:12

Your code logic is odd. Scriptlets also doesn't make testing more easy. Here's an SSCCE in flavor of a real Java class to kickoff and ease testing:

public class Test {

    public static void main(String[] args) throws Exception {
        String user = "WeBeNg"; // Change this as if it is user input.
        String pass = "2009"; // Change this as if it is user input.

        String expectedUser = "webeng";
        String expectedPass = "2009";

        if (user == null || pass == null || user.isEmpty() || pass.isEmpty()) {
            System.out.println("Please enter both username and password.");
        } else if (user.equalsIgnoreCase(expectedUser) && pass.equals(expectedPass)) {
            System.out.println("Welcome " + user);
        } else {
            System.out.println("Unknown login.");
        }
    }

}

Hope this helps.

Edit #1: please do not post screenshots. Copypaste the exception and stacktrace in code blocks. The exception is by the way not coming from Eclipse. It's coming from Tomcat. Also, the exception in question (NullPointerException) is fairly self-explaining. You accessed an object reference which is actually null.

SomeObject someObject = null;
someObject.doSomething(); // Fails with NPE.
someObject = new SomeObject();
someObject.doSomething(); // Succes.

You need to do a null-check first, e.g.

if (someObject != null) {
    someObject.doSomething(); // Succes.
} 

Edit #2 I also recommend you to learn about operators, operator precedence and expression grouping in Java. See, the following isn't "logical"

if (name !=null | pass!=null && name.equals("") | pass.equals(""))

Here's a good tutorial to start with to learn about this: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html Good luck.

You can use compareToIgnoreCase and you don't have to do the conversion yourself.

You seem to use | in stead of || . Is that intentional?

Here it is with equalsIgnoreCase. Unless you really don't want to shortcut the or with the | operator it's generally safer to go with ||. I also prefer not to fall through to success as in BalusC's answer, there is something unsettling about the default condition being to "login" the user.

<% 
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String globalname = "webeng";
String globalpass = "2009";
if (name ==null || pass==null || name.equals("") || pass.equals("")) {
// You can use StringUtils.isBlank here instead if you have it available to you
 %>
<hr />
 <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
 <hr />
<% } else if (name.equalsIgnoreCase(globalname) && pass.equalsIgnoreCase(globalpass))
 {
 %>
  <hr />
   <p><b>Howdy, <%= request.getParameter("name") %></b></p>
  <hr />
<% } else {
 %>
  <hr />
   <p><b>Incorrect Userdata!</b></p>
  <hr />
 }
%>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!