Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将节点与java中的字符串进行比较_Java_User Interface_Search_Netbeans_Nodes - Fatal编程技术网

将节点与java中的字符串进行比较

将节点与java中的字符串进行比较,java,user-interface,search,netbeans,nodes,Java,User Interface,Search,Netbeans,Nodes,如您所见,我正在尝试在堆栈中搜索某个字符串 我的堆栈如下所示:1、2、3、4、5。 方法正在比较以下值:未找到:menuGUI$Node@4ae17538三个 当使用.equals时,程序将永远循环 添加中断,并将==更改为。等于遵循以下代码: String target = JOptionPane.showInputDialog("Please enter the string you are looking for"); Node current = top; int

如您所见,我正在尝试在堆栈中搜索某个字符串

我的堆栈如下所示:
1、2、3、4、5。

方法正在比较以下值:
未找到:menuGUI$Node@4ae17538三个


当使用.equals时,程序将永远循环

添加
中断之后执行code>,并将
==
更改为
。等于

遵循以下代码:

    String target = JOptionPane.showInputDialog("Please enter the string you are looking for");
    Node current = top;
    int counter = 0;

    while (current != null) {
        if (current.getElement() == (target)) {
            printTextField.setText(target + " was found, its position is: "+ counter);
        } else {
            System.out.println("not found: "+current +" "+target);
    current = current.getNext();
            counter ++;
        }
    }
请注意区别:

  • 所有对象类型都必须与
    equals()
    方法进行比较。这包括
    字符串
    ,因为它也是一种对象类型
  • 使用
    break
    停止循环,因为您已经找到了结果,所以无需继续循环

我建议您使用helper变量
boolean flagFound
,并根据其值显示文本。您是否真的想写出例如1000次在1、2、3日未找到的目标。。第十二。。。176指数

while (current != null) {
    if (current.getElement().equals(target)) {
        printTextField.setText(target + " was found, its position is: "+ counter);
        break;
    } else {
        System.out.println("not found: "+current +" "+target);
        current = current.getNext();
        counter ++;
    }
}

先学习基础知识给出一个引用比较,您想比较对象的值。
current.getElement()
的类型是什么?另外,当发现发生时,您应该
循环中中断
。@TDG它的字符串Berger实际上解决了我的问题,没有输入break和now的愚蠢错误。equals工作得很好。谢谢,关于打印
目标的部分尚未找到,它仅用于我自己,用于调试。
boolean flagFound = false;

while (current != null) {
    if (current.getElement() == (target)) {
        printTextField.setText(target + " was found, its position is: "+ counter);
        flagFound = true;
        break;
    } 
    current = current.getNext();
    counter ++;
}

if (!flagFound) {
    System.out.println("The target " + target + " haven't been found.");
}