当前位置: 首页 > 面试题库 >

线程“主”中的异常java.util.NoSuchElementException:找不到行-使用扫描仪输入

谯灿
2023-03-14
问题内容

我正在尝试从链接列表中删除特定节点。我试图调用我的方法removeNode,但是当我调用它来获取用户输入时,它给了我这个错误。任何有关如何解决此问题的建议将不胜感激!

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at LinkedList.removeNode(LinkedList.java:123)
    at fileIn.<init>(fileIn.java:22)
    at fileIn.main(fileIn.java:13)

LinkedList类:

import java.util.Scanner;

public class LinkedList {

    public LinkedListNode front;

    public LinkedList() {
        this.front = null;
    }

    public void insertBack(String data)
    {
        if(front == null){
            front = new LinkedListNode(data);
        }
        else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = front;

            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }
    }//end insertBack

    public void addAfter(LinkedListNode spot, String data)
    {
        LinkedListNode newNode;

        newNode = new LinkedListNode(data);

        newNode.next = spot.next;
        spot.next = newNode;
    }//end addAfter

    public void addBefore(LinkedListNode spot, String data)
    {

    }//end addBefore

    public void deleteAfter(LinkedListNode spot)
    {
        LinkedListNode nextNode;

        nextNode = spot.next;
        spot.next = nextNode.next;
    }//end deleteAfter


    public String showList()
    {
        sortList();
        //^-- Will sort the sum but not the nodes

        int i = 0;
        String retStr = "The nodes in the list are:\n";
        LinkedListNode current = front;
        while(current != null){
            i++;
            retStr += "Node " + i + " is: " + current.getData() + " and the sum is: " + current.getSum() + "\n";
            current = current.getNext();
        }

        return retStr;
    }

    public LinkedListNode findTail()
    {
        LinkedListNode current = front;
        while(current.getNext() != null){
            current = current.getNext();
        }
        return current;
    }//end findTail

    public LinkedList sortList()
    {
        LinkedListNode current = front;
        LinkedListNode tail = null;

        while(current != null && tail != front )
        {
            LinkedListNode next = current;

            for( ; next.next != tail;  next = next.next)
            {
                if(next.sum >= next.next.sum)
                {
                    long temp = next.sum;
                    String temp2 = next.data;

                    next.sum = next.next.sum;
                    next.data = next.next.data;

                    next.next.sum = temp;
                    next.next.data = temp2;
                }
            }

            tail = next;
            current = front;
        }

        return this;
    }

    public void removeNode(){

    String searchedNode;

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the name you would like to remove from the list: ");
    searchedNode = in.nextLine();
    in.close();
        LinkedListNode previous = null;
        LinkedListNode current = front;

            //while there is something in the list nodes
             while (current != null)
             {
                //if the data of current is equal to the node being searched
                if(current.data.equals(searchedNode))
                {
                  //set current to the node after current
                  current = current.next;
              //if previous is equal to null(which it is)
              if (previous == null)
                  {
                  //set previous equal to current
                  previous = current;
                  }

                  else previous.next = current;
                } else {
                  previous = current;
                  current = current.next;
            }
        } //end while
    }
}

fileIn类别:

import java.util.Scanner;
import java.io.*;

public class fileIn
{
   LinkedListNode front;
   LinkedList myList = new LinkedList();
   String fname;

   public static void main(String[] args)
    {
       fileIn f = new fileIn();
   }//end main


   public fileIn()
   {
      getFileName();
      readFileContents();
      System.out.print(myList.showList());
      myList.removeNode();
   }//end fileIn

   public void readFileContents()
    {
        boolean looping;
        DataInputStream in;
        String line;

        /* Read input from file and process. */
        try
        {
            in = new DataInputStream(new FileInputStream(fname));

            looping = true;
            while(looping)
             {
                /* Get a line of input from the file. */
                if (null == (line = in.readLine()))
                {
                    looping = false;
                    /* Close and free up system resource. */
                    in.close();
                }//end if
                else
                {
                myList.insertBack(line);
                }//end else
            } /* End while. */
        } /* End try. */

        catch(IOException e)
        {
            System.out.println("Error " + e);
        } /* End catch. */
    }//end readFileContents

     public void getFileName()
     {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter file name please.");
        fname = in.nextLine();
        in.close();
     }//end getFileName

}//end class fileIn

问题答案:

问题是您要关闭System.inScanner.close()
关闭基础流)。一旦执行此操作,它将保持关闭状态,并且无法输入。您通常不希望使用标准输入来执行此操作:

String searchedNode;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name you would like to remove from the list: ");
searchedNode = in.nextLine();
// in.close(); // <-- don't close standard input!

另外,为了将来参考,您应该尝试创建更多的最小测试用例。这将帮助您调试并消除问题中的许多噪音。:-)



 类似资料:
  • 肯定有一条线,但我不明白为什么扫描仪看不到… 以下是文件的开头: 下面是我获取它的代码: 但我得到了错误: book1_enc的文件是我的LZW编码算法的输出。当我将文件传递给我的解码器时,我希望解码器知道字典的大小,在这种情况下是256...感谢您的阅读...

  • 问题内容: 我收到以下异常。 java.util.NoSuchElementException:找不到行 我在编写一个需要从文本文件读取的较大程序时遇到了此错误,因此决定进行测试。 而且我仍然得到例外。我在与名为stricts.txt的类相同的文件夹中有一个文本文件,其中包含文本。我究竟做错了什么? 问题答案: 新的File(“ restrictions.txt”)将在应用程序的“开始目录”中查找

  • 我对的体验非常糟糕,因为我使用的是和。所以我不能做扫描仪的程序。我是新手,所以请帮助我,“找不到”。这是我到目前为止的代码。

  • 我正在做一个场景,我只想接受1或2作为输入,并且在输入另一个数字或输入无效时处理错误。为此,我正在做: 如果我添加在中,由于选项的范围在try内,它会给出一个错误。我希望它继续要求用户输入有效数字,即1或2,但如果我输入任何字符,它将进入并退出。

  • 我刚开始我的大学java课程,在扫描器类中不断得到这个错误。 我不断得到的错误是:

  • 我试图让一个扫描器读取文本文件的输入,将该输入放入一个字符串,为该字符串设置一个StringTokenizer,然后设置一个String[],该数组的每个元素都是该StringTokenizer的标记。这样做的目的是从文本文件中获取输入文本的String[],以便数组的每个元素都是文本文件中的一个单词。但是,到目前为止,我的代码生成了一个NoSuchElementFound异常。 为什么会发生这种