简体   繁体   English

如何在Java中为单词分配数字?

[英]How to assign numbers to words in java?

I have a problem about the last part of the code. 我对代码的最后一部分有疑问。 I want to assign numbers to specific words but i always get 0 value, even though I get those strings from the first System.out.println correctly, i cannot get the numerical equivalents of those strings at the second System.out.println.Any ideas how to solve this problem? 我想为特定的单词分配数字,但是即使我从第一个System.out.println正确获取了这些字符串,我也始终获得0值,但是在第二个System.out.println.Any中却无法获取这些字符串的数字等效项想法如何解决这个问题?

public static double number;

protected void myMethod(HttpServletRequest request, HttpServletResponse response) {

    String speech= request.getParameter("speech");
    System.out.println("The recognized speech is : "+ speech);

    // There is no problem till here.
    if(speech == "Hi")
        number = 1 ;
    if(speech== "Thanks")
        number = 2 ;
    if(speech== "Bye")
        number = 0 ;

    System.out.println("The number for the speech is : " + number);
}

However here i dont get the correct numbers but only 0 for each word! 但是在这里我没有得到正确的数字,但是每个单词只有0!

The == will only be true if the Strings are the same object. 仅当字符串是同一对象时,==才为true。 Use: 采用:

if(speech.equals("Hi"))

or to match without case: 或匹配而不区分大小写:

if(speech.equalsIgnoreCase("hi"))

You can't use the == operator to check if two Strings have the same value in Java, you need to use the .equals() or equalsIgnoreCase() methods instead: 您不能使用==运算符来检查两个字符串在Java中是否具有相同的值,而是需要使用.equals()equalsIgnoreCase()方法:

if("Hi".equalsIgnoreCase(speech)) {
    number = 1;
}
else if("Thanks".equalsIgnoreCase(speech)) {
    number = 2;
}
else if("Bye".equalsIgnoreCase(speech)) {
    number = 0;
}
else {
    number = -1;
}

The reason for this is that the == operator compares references ; 原因是==运算符比较引用 that is it will return true if and only if the instance stored in variable speech is the same instance as the literal String you've created between double quotes ( "Hi" , "Thanks" , or "Bye" ). 那就是它会返回true当且仅当存储在变量实例 speech同一个实例为你双引号(之间产生的文本字符串"Hi""Thanks" ,或"Bye" )。

Note also that I use the equalsIgnoreCase() call on the literal String I'm declaring, rather than the variable that is assigned from the parameter. 还要注意,我在声明的文字字符串上使用equalsIgnoreCase()调用,而不是从参数分配的变量上使用equalsIgnoreCase()调用。 This way, if a speech == null , the method call is still valid ( "Hi" will always be a String ), and so you won't get a NullPointerException , and the flow will continue until the else branch. 这样,如果speech == null ,则方法调用仍然有效( "Hi"始终String ),因此您将不会收到NullPointerException ,流程将继续进行,直到else分支为止。

Try the following snippet: 尝试以下代码段:

Main.java Main.java

public class Main {
    public static void main(String[] args) {
        List<StringWithValue> stringList = new ArrayList<StringWithValue>();
        stringList.add(new StringWithValue("Hi", 1));
        stringList.add(new StringWithValue("Thanks", 2));
        stringList.add(new StringWithValue("Bye", 3));

        String speech = "Hi";
        int number = 0;

        for(StringWithValue swv : stringList){
            if(swv.getString().equals(speech)){
                number = swv.getValue();
                break;
            } else {
                number = -1;
        }

        System.out.println("The number for the speech is : " + number);
    }
}

StringWithValue.java StringWithValue.java

public class StringWithValue {
    private String string;
    private int value;

    public StringWithValue(String string, int value) {
        this.string = string;
        this.value = value;
    }

    public String getString() {
        return string;
    }

    public int getValue() {
        return value;
    }
}
public static double number;

    if(speech=="hi")
    {
        number=1;
    }
    else if(speech=="thanks")
    {
        number=2;
    }
    else if(speech=="Bye")
    {
        number=0;
    }
    else
    {
        System.out.println("Word Not Found");
    }

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

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