我正在尝试使用扫描仪从文本文件中提取令牌,文本文件的名称为“ElectricToolData.txt”,
文本文件的内容:
// this is a comment, any lines that start with //
// (and blank lines) should be ignored
// data is rechargeable, power, toolName, itemCode, timesBorrowed, onLoan, cost, weight
true,18V,Makita BHP452RFWX,RD2001,12,false,14995,1800
true,10.8V,Flex Impact Screwdriver FIS439,RD2834,14,true,13499,1200
false,1350W,DeWalt D23650-GB Circular Saw, RD6582,54,true,14997,5400
false,1500W,Milwaukee DD2-160XE Diamond Core Drill,RD4734,50,false,38894,9000
true,10.8V,Bosch GSR10.8-Li Drill Driver,RD3021,25,true,9995,820
false,900W,Bosch GSB19-2REA Percussion Drill,RD8654,85,false,19999,4567
true,10.8V,Flex Impact Screwdriver FIS439, RD2835,14,false,13499,1200
true,18V,DeWalt DW936 Circular Saw,RD4352,18,false,19999,3300
false,2100W,Sparky FK652 Wall Chaser,RD7625,15,false,29994,8400
以下是我尝试使用不成功的扫描程序从文本文件中提取令牌的尝试:
错误消息(行扫描程序扫描程序):未报告的异常java.io.FileNotFoundException;必须被抓或宣布被抛出。
public void extractTokens()
{
//extracts tokens from the text file
File text = new File("E:/LEWIS BC 2/project 1/ElectricToolData.txt");
Scanner scanner = new Scanner(text);
String toolName = scanner.next();
String itemCode = scanner.next();
String power = scanner.next();
String timesBorrowed = scanner.next();
String onLoan = scanner.next();
String cost = scanner.next();
String weight = scanner.next();
//System.out.println(parts.get(1)); // "en"
}
任何回复或帮助都会非常感激,因为我真的很困惑。
答案 0 :(得分:0)
抛出或捕获" FileNotFoundException"
我怀疑你没有使用像Eclipse或IntelliJ这样的IDE。它会在那里发出编译警告。无论如何这个代码应该工作
public void extractTokens() throws FileNotFoundException
{
// extracts tokens from the text file
File text = new File("E:/LEWIS BC 2/project 1/ElectricToolData.txt");
Scanner scanner = new Scanner(text);
String toolName = scanner.next();
String itemCode = scanner.next();
String power = scanner.next();
String timesBorrowed = scanner.next();
String onLoan = scanner.next();
String cost = scanner.next();
String weight = scanner.next();
// System.out.println(parts.get(1)); // "en"
}
答案 1 :(得分:0)
您可能会遇到IOException和/或FileNotFoundException,因此您应该捕获这两个:
public void extractTokens() throws IOException, FileNotFoundException{
}