gpt4 book ai didi

java - 输入不匹配异常错误,

转载 作者:行者123 更新时间:2023-11-30 03:08:23 26 4
gpt4 key购买 nike

程序编译并完美运行,直到我尝试在 main 中执行我的加载方法。程序崩溃并给我一个输入不匹配异常零件编号 = scan.nextInt(); .....有人知道为什么吗?

public static InventoryManager load(String fileName) throws IOException,ClassNotFoundException
{
Scanner fileScan = new Scanner (new File(fileName));
Scanner stringScan;
InventoryManager StockChart = new InventoryManager();

// Part variables
String record = "";
int partNumber=0;
String description="";
int qty=0;
double cost = 0.00;


while(fileScan.hasNext())
{
record = fileScan.nextLine();
stringScan = new Scanner (record);
stringScan.useDelimiter(" "); //allows for separation when reading
partNumber = stringScan.nextInt(); // scans part number
description = stringScan.next(); // scans description
qty = stringScan.nextInt(); // scans the qty on hand
cost = stringScan.nextDouble(); // scans the item cost

//create new part object for each line in file
StockChart.addStock(new Stock(partNumber,description, qty,cost));

}


return StockChart; // return new list back to InventoryClerk program
}

文本文件的格式如下(忽略之间的空格):

1117[选项卡]1/2-13 FHN[选项卡]450[选项卡]6.11

1118[tab]1/2-13 FHN[tab]100[tab]0.23

1119[选项卡]1/2-13 FHN[选项卡]100[选项卡]4.11

最佳答案

比使用 stringScan Scanner 对象更好的方法是简单地使用 String.split记录字符串

例如

while(fileScan.hasNext())
{
record = fileScan.nextLine();
String el[] = record.split (" ");
partNumber = Integer.parseInt (el[0]);
description = el[1];
// etc

关于java - 输入不匹配异常错误,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191998/

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