gpt4 book ai didi

java - 使用扫描仪类读取 .txt 时出错

转载 作者:行者123 更新时间:2023-12-02 07:07:15 28 4
gpt4 key购买 nike

我正在开发一个程序来读取二进制文件并将其更新为 txt 文件,它正在工作,突然开始抛出此错误

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at StockManage.updateInv(StockManage.java:134)
at StockManage.main(StockManage.java:173)

这是导致错误的代码

try
{
Scanner trans = new Scanner(new File(file));
RandomAccessFile inv = new RandomAccessFile (file2,"rws");
String tempName = "Temp" + (int)(Math.random()*1000000) + ".dat";
RandomAccessFile newInv = new RandomAccessFile (tempName , "rws");
File newFile = new File(tempName);

String transISBN = trans.next();
String author = inv.readUTF();
String title = inv.readUTF();
String iSBN = inv.readUTF();
int amount = inv.readInt();

while (inv.getFilePointer()<=inv.length())
{
boolean empty = true;

while (empty&&trans.hasNext())
{

if (iSBN.compareTo(transISBN)<0)
{
empty = false;
break;
}
else if (iSBN.compareTo(transISBN)==0)
{
int change = trans.nextInt();
amount += change;
transISBN = trans.next();
}
}

newInv.writeUTF(author);
newInv.writeUTF(title);
newInv.writeUTF(iSBN);
newInv.writeInt(amount);

author = inv.readUTF();
title = inv.readUTF();
iSBN = inv.readUTF();
amount = inv.readInt();


}

我真的陷入了困境,所以任何帮助都会很棒

最佳答案

在 Scanner 对象上调用 next() 之前,您应该调用 hasNext() 来检查是否确实有更多数据要读取。

关于java - 使用扫描仪类读取 .txt 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15953112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com