- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
<分区>
我正在尝试从链表中删除特定节点。我正在尝试调用我的方法 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)
链表类:
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
}
}
类中的文件:
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
此查询由 2 个相关问题组成。在进行下一步之前,我需要等待元素不可见,因此我尝试定义一个自定义方法,如下所示: public void waitToDisappear(long timeOutInSe
所以我有一个一年前编写的相当大的 java 应用程序,我试图再次理解它。我正在查看代码中的一个方法,其中明显存在出现 NoSuchElementException 的风险:我正在使用任意字符串构造的扫
我开始通过简单地读取数据文件来做练习。当我运行该程序时,数据文件被读取但没有读取,但出于某种原因我仍然收到“NoSuchElementException”并且我的输出没有按照预期的方式格式化。这是正在
我正在尝试逐行读取一个大型 csv 文件,以查找其中字符串出现的次数。 这是执行此操作的代码: public int getOffset(File file, String searched) thr
我的代码在使用扫描仪的每一行上都给出了 NoSuchElementException 异常。我的其余代码工作正常。 import java.io.*; import java.util.*; impo
当我运行我的程序时,我在 Eclipse 控制台中得到了这个: Type a number then press enter. 1. Add a customer 2. Add an order 3.
我想知道是否有人可以帮助解决我的程序中的 NoSuchElements 异常,该程序扫描非常大的文本,然后将其添加到 ArrayList 中。 我尝试重新排列代码的顺序,看看是否可以修复它,但现在我不
for (final ArrayList smartPhones : smartPhonesCluster) { new Thread(new Runnable() { @Ov
我想在客户端读取时设置超时。该例程应该抛出 InterruptedIOException,但它在 System.out.println("echo: " + _in.nextLine()); 上抛出
我在功能检查冲突方面遇到问题。无论我做什么,它都会抛出 NoSuchElementException。这是: public boolean isColliding(Pixel a) { if(
如果在读取文件时缺少字段,我应该捕获 NoSuchElementException。如果缺少一个字段,我只需要跳到文件的下一行。我的问题是,我在哪里实现我的 try/catch 代码来做到这一点?这是
我正在开发一种解析器(业余爱好项目),它采用 Cpp 文件,读取文件中的注释,然后尝试基于该文件创建头文件。 我面临的问题是当java.util.Scanner即将读取第一行时。程序停止并给出 NoS
我的任务是: 创建一个包含 2 列数字的文件:距离和速度。 编写一个 TravelInfo 类,其中包含三部分信息:速度、时间、距离。 该类还应该有一个方法 calcTime(),用于根据距离和速度计
在过去的几个小时里,我一直对迭代器感到震惊。 当我执行Iterator时,得到NoSuchElementException 代码 new Thread() { @Overrid
我尝试了许多不同的方法来修复该错误。我知道这与 hasNext 有关,我只是不确定错误在哪里。请帮忙。另外,如果有人能告诉我如何从 getLargestQuantityItem 方法将变量larges
代码在 Gitbash 命令行 (GitHub) 中正确运行。但是,当我在 NetBeans 中编译并运行它时,它给出了 NoSuchElement Exception。如果有人可以帮助我使用 Net
从队列中删除时,我有时会收到 NoSuchElementException 。我需要使用锁/等待/其他一些延迟机制吗? 我提供了代码的粗略翻译。 带队列的线程 public void run(){ w
如何删除代码中的 NoSuchElementException ?我也在应用 hasNextLine 但它也没有接受它。 public static void main (String[] a
我已经做了一些研究,但一无所获。 这是我的代码: public void play(){ System.out.print("Make your choice ( R - P - S) : "
自从我醒来以来,我一直在尝试让它工作,但我根本无法弄清楚是什么导致了这个异常。 try { scan = new Scanner(new File("dictionary.txt"))
我是一名优秀的程序员,十分优秀!