gpt4 book ai didi

java - TomCat - 构造函数在尝试从文件中读取时抛出异常

转载 作者:行者123 更新时间:2023-11-28 23:23:15 25 4
gpt4 key购买 nike

我有下面的一些方法,基本上我的问题是当我尝试从 .txt 文件中读取时。该应用程序在内存中运行良好。当我将 decode() 添加到重载构造函数时,我在 Tomcat 服务器日志中收到以下错误:

Constructor threw exception; nested exception is java.lang.NumberFormatException: For input string: ""

当我仔细查看它时,它指向解码方法中的这一行currentDVD.setDvdId(Integer.parseInt((currentTokens[0]))); 但我似乎无法弄清楚问题是什么。有一次我能够从文件中读取,然后我试图让编码方法工作,但在某个时候发生了一些事情。任何帮助将不胜感激。

public class DvdLibraryInFileImpl implements DvdLibraryDao {

private Map<Integer, DVD> dvdMap = new HashMap<>();
public static final String DVD_FILE = "dvd.txt";
public static final String DELIMITER = "::";
private static int dvdIdCounter = 0;

public DvdLibraryInFileImpl() throws FileNotFoundException {
decode();

}


@Override
public DVD addDVD(DVD dvd) {

dvd.setDvdId(dvdIdCounter);

dvdIdCounter++;

dvdMap.put(dvd.getDvdId(), dvd);

return dvd;
}

@Override
public DVD getDVDById(int dvdId) {
return dvdMap.get(dvdId);
}

@Override
public List<DVD> getAllDVDSByName(String searchByName) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public List<DVD> getAllDVDS() {
List<DVD> allDVDS = new ArrayList<>(dvdMap.values());
return allDVDS;
}

@Override
public void updateDVD(DVD dvd) {
dvdMap.put(dvd.getDvdId(), dvd);
}

@Override
public void removeDVD(int dvdId) {
dvdMap.remove(dvdId);
}


@Override
public void decode() throws FileNotFoundException {
Scanner sc = new Scanner(new BufferedReader(new FileReader(DVD_FILE)));
String[] currentTokens;
while (sc.hasNextLine()) {
String currentLine = sc.nextLine();
currentTokens = currentLine.split(DELIMITER);

DVD currentDVD = new DVD();

currentDVD.setDvdId(Integer.parseInt((currentTokens[0])));
currentDVD.setTitle(currentTokens[1]);
currentDVD.setReleaseDate(currentTokens[2]);
currentDVD.setMpaaRating(currentTokens[3]);
currentDVD.setDirectorsName(currentTokens[4]);
currentDVD.setStudio(currentTokens[5]);
currentDVD.setUserRating(currentTokens[6]);

dvdMap.put(Integer.parseInt((currentTokens[0])), currentDVD);
}

}


@Override
public void encode() throws IOException {
PrintWriter out = new PrintWriter(new FileWriter(DVD_FILE));
Set<Integer> keySet = dvdMap.keySet();
for (Integer i : keySet) {

out.print((dvdMap.get(i)).getDvdId());
out.print(DELIMITER);

out.print((dvdMap.get(i)).getTitle());
out.print(DELIMITER);

out.print((dvdMap.get(i)).getReleaseDate());
out.print(DELIMITER);

out.print((dvdMap.get(i)).getMpaaRating());
out.print(DELIMITER);

out.print((dvdMap.get(i)).getDirectorsName());
out.print(DELIMITER);

out.print((dvdMap.get(i)).getStudio());
out.print(DELIMITER);

out.print((dvdMap.get(i)).getUserRating());
out.println("");

}
out.flush();
out.close();
}
}

最佳答案

问题是因为 currentLine 没有收到正确的输入数据(来自用户输入)。

您应该确保 currentTokens 设置了正确的 DvdId(应该是数值),否则 Integer.parseInt((currentTokens[0])) 行将抛出 NumberFormatException 用于非数字或空 ("") 数据。

您需要确保输入的数据与数字 dvdid 正确无误。

例如,输入以下数据:1234::DVDTITLE::29-OCT-2016::RATING1::DIRECTOR::STUDIO::RATING2

我强烈建议您添加输入验证来处理输入非数字值或评分高于最大值或导演姓名包含数字值等场景......

关于java - TomCat - 构造函数在尝试从文件中读取时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322626/

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