将键入的文本转换为小写 [英] Convert typed-in Text to lowercase

查看:111
本文介绍了将键入的文本转换为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个index.jsp,

I've got an index.jsp with

[snip]

<% 
  String name = request.getParameter("name");
  String pass = request.getParameter("pass");
  String globalname = "webeng";
  String globalpass = "2009";
  if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
   {
   %>
    <hr />
    <p><b>Howdy, <%= request.getParameter("name") %></b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
  {
  %>
    <hr />
    <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
  {
  %>
    <hr />
    <p><b>Incorrect Userdata!</b></p>
    <hr />
<% }
  else{
  }
%>

[snip]

现在,例如,globalname是小写的webeng。人们可以输入WebEng,webENG,WEBENG及其变体。

Now, the globalname for example is in lowercase "webeng". Folks may type in "WebEng", "webENG", "WEBENG" and variations thereof.

我需要将字符串转换为小写字母。不知何故

I need those typed in Strings converted to lowercase. Somehow

String newname = name.toLowerCase();
String newpass = pass.toLowerCase();

无效。有人知道吗?

这就是Eclipse在我使用时告诉我的

This is what Eclipse tells me when I use

<% 
      String name = request.getParameter("name");
      String pass = request.getParameter("pass");
      String globalname = "webeng";
      String globalpass = "2009";
      String newname = name.toLowerCase();
      String newpass = pass.toLowerCase();

       if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
       {
       %>
        <hr />
        <p><b>Howdy, <%= request.getParameter("name") %></b></p>
        <hr />
    <% }
      else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
      {
      %>
        <hr />
        <p><b>One or more fields are empty!</b></p>
        <hr />
    <% }
      else if (name !=null && pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
      {
      %>
        <hr />
        <p><b>Incorrect Userdata!</b></p>
        <hr />
    <% }
      else{
      }
    %>

Eclipse: http://i.imagehost.org/0277/2009-11-15_19_34_00.png

推荐答案

您的代码逻辑很奇怪。 Scriptlet也不会使测试更容易。这是一个真正的Java类的 SSCCE ,以启动和简化测试:

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.");
        }
    }

}

希望这个帮助。

编辑#1 :请不要发布截图。在代码块中共享异常和堆栈跟踪。顺便提一下,不是来自Eclipse。它来自Tomcat。此外,有问题的例外(NullPointerException)是相当自我解释的。您访问了一个实际为null的对象引用。

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.
} 

编辑#2 我还建议你学习关于Java中的运算符,运算符优先级和表达式分组。看,以下不是逻辑

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(""))

这是一个很好的教程开始了解这一点: http://java.sun。 com / docs / books / tutorial / java / nutsandbolts / operators.html 祝你好运。

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.

这篇关于将键入的文本转换为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆