[英]Reading from input(keyboard) using Scanner.nextLine()
I am implementing sequential search using Java. 我正在使用Java实现顺序搜索。 I am trying to search string from a string array.
我试图从字符串数组中搜索字符串。 The query is obtained from keyboard using
nextLine()
. 使用
nextLine()
从键盘获取查询。 However I always get "not found" even when the string is clearly in the list. 但是,即使字符串明显在列表中,我也总是“找不到”。
/**
Implementing sequential search
*/
public class SequentialSearch {
public static boolean sequentialSearch(String[] names, String query) { //static method takes a string array and the query as arguments
for (String x: names) //traverse the list
if (x == query) {
System.out.println("found");
return true;
} //end if
System.out.println("not found"); //end for
return false;
} //end method
} //end class
class TestSequentialSearch {
public static void main (String[] args) {
String[] names = {"John", "Amy", "Tom", "Jay", "Olivia", "Jack", "Peter", "Emma"}; //a new name list
Scanner in = new Scanner(System.in);
String x;
System.out.println(Arrays.toString(names));
System.out.println("name to be searched: ");
while (in.hasNextLine()) {
x = in.nextLine();
SequentialSearch.sequentialSearch(names, x); //search input in the list
System.out.println("name to be searched: ");
} //end while
} //end main
} //end test
Use equals() method instead of == for string comparisions. 使用equals()方法而不是==进行字符串比较。 As equals check for string contents equality while == checks for objects equality.
当equals检查字符串内容相等时,==检查对象是否相等。 Change your code
if
condition: if
条件更改您的代码:
From 从
if (x == query)
to 至
if (x.equals(query))
Learn more about the difference between equals and == comparision for string from the related post here: 从相关帖子中了解有关equals和==比较字符串之间差异的更多信息:
You need to use .equals() to compare Strings in Java, rather than ==. 您需要使用.equals()来比较Java中的字符串,而不是==。 This is because in Java, a string is an object and == would compare the object reference - so would only be true if you had two variables pointing to the same String object.
这是因为在Java中,字符串是一个对象,而==会比较对象引用 - 所以只有当你有两个指向同一个String对象的变量时才会这样。
.equals() on the other hand, compares the object CONTENT, so would look at the actual content of the string, and return true if they were equal. 另一方面,.equals()比较对象CONTENT,因此将查看字符串的实际内容,如果它们相等则返回true。
This is the line I'm referring to: 这是我所说的那句话:
if (x == query)
In Java, for non-primitive types, the ==
operator compares references, not values. 在Java中,对于非基本类型,
==
运算符比较引用,而不是值。
If you create a bunch of equivalent String literals, like: 如果您创建了一堆等效的字符串文字,例如:
String sLit1 = "test";
String sLit2 = "test";
(sLit1 == sLit2)
will be true
, since Java doesn't allocate new memory for each new String
literal, it just points them all to the same location in memory. (sLit1 == sLit2)
将为true
,因为Java不为每个新的String
字符分配新内存,它只是将它们全部指向内存中的相同位置。 However, when you create a String
object: 但是,在创建
String
对象时:
String sObj = new String("test")
it will occupy a new location in memory. 它将在内存中占据一个新的位置。 So
(sLit1 == sObj)
will always be false
. 所以
(sLit1 == sObj)
总是false
。
Which means that ==
yields true if and only if the two arguments refer to the same object. 这意味着
==
当且仅当两个参数引用同一个对象时才产生true。 To compare strings, use equals method, as in sObj.equals(sLit1)
, in your case x.equals(query)
. 要比较字符串,请使用equals方法,如
sObj.equals(sLit1)
,在您的情况下使用x.equals(query)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.