我正在尝试为我的 Java 类编写一个 Inventory 程序。该程序需要从.txt
文件中读取初始库存数据(迭代一定次数 - 由inv.txt
我们正在使用的文件中的第一个整数定义)。我的文本文件是正确的,扫描仪、数组和循环似乎是正确的,但是我得到了 InputMismatchException。
.txt 文件格式如下:
XXXX - Count - # of times to iterate
XXXX - Product Code
XXXX -Quantity On Hand
XXXX - Reorder Level
在对每个产品重复产品代码、QOH 和再订购级别的情况下,COUNT 仅在文件中出现一次。
必须读取所有值,并将每个值存储在各自的数组中。
import javax.swing.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import java.*;
import java.io.*;
public class Lab7Test2
{
public static void main (String [] args) throws IOException
{
int count = 0; //To hold Max Count (Max Iterations Expected)
int countAt = 0; //To hold Current Count.
int number = 0; //To hold a number.
int number2 = 0; //To hold a 2nd Number.
int index = 0; //Index Placeholder.
int index2 = 0;
int index3 = 0;
int[] partNumb;
int[] qoh; //Holds Product's Quantity On Hand (QOH)
int[] reorder; //Minimum Reorder Level
int[] transNumb;
int[] transType;
int[] transAmt;
String[] status; //Holds Product's Inventory Status as String.
String[] error; //Holds Error Messages associated with Transactions.
String input;
String output; //Holds output for transaction Log.
Scanner keyboard = new Scanner(System.in);
File openFile;
Scanner scanFile;
//----End of Variable Declaration--- /////////////////////////////////////////////
//----Begin Program Execution----////////////////////////////////////////////////////
System.out.println("Enter the Inventory File Name.");
input = keyboard.nextLine();
if(!input.contains(".txt")) //If Input has no '.txt' extension, error message.
{
while(!input.contains(".txt")) //Repeat error if no '.txt' extension found.
{
System.out.println("Invalid Input");
System.out.println("Enter the Inventory File Name.");
input = keyboard.nextLine();
}
openFile = new File(input); //Set openFile to 'input' if '.txt' extension found.
scanFile = new Scanner(openFile);
System.out.println("File Loaded.");
}
else
{
openFile = new File(input); //Set openFile to 'input' if '.txt' extension found.
scanFile = new Scanner(openFile);
System.out.println("File Loaded.");
}
number = scanFile.nextInt();
number *= 3;
partNumb = new int[number]; //Set partNumb[] Size = to count
qoh = new int[number]; //Set qoh[] Size = to count
reorder = new int[number]; //Set reorder[] Size = to count
count = number;
number = 0;
引发异常的是以下循环的开始,特别是向下的 5 行:number 2 = scanFile.nextInt()。只有当我将“计数”和“数字”变量设置为 * 3 时,我才会收到此错误(以确保每个产品都有 3 个值:产品代码、手头数量、重新订购级别)。
while(countAt < (count * 3))
{
if(number == 0) // Number 0 = partNumb[]
{
number2 = scanFile.nextInt();
partNumb[index] = number;
index++;
number++;
countAt++;
}
else if(number == 1) //Number 1 = qoh[]
{
number2 = scanFile.nextInt();
qoh[index2] = number;
index2++;
number++;
countAt++;
}
else if(number == 2) //Number 2 = reorder[]
{
number2 = scanFile.nextInt();
reorder[index3] = number;
index3++;
number = 0;
countAt++;
}
}
System.out.println("Data Loaded to Arrays"); //Confirmation of Data Acceptance.
//Reset all Counter & Index Variables for use with next Loop.
index = 0;
index2 = 0;
index3 = 0;
countAt = 0;
number = 0;
number2 = 0;
while(countAt < (count * 3))
{
System.out.println(partNumb[index]); //Print All Values in the partNumb[] Array.
index++;
countAt++;
}
//----END PROGRAM -----//////////////////////////////////////////////////////////////
}
}
根据至少 3 天的问题研究,我已经尝试了无数种方法来纠正这个问题,但到目前为止还没有想出任何方法来解决这个 InputMismatchException 错误?. 如果有人有任何建议,请告诉我,谢谢。
堆栈跟踪:
----jGRASP exec: java Lab7Test2
Enter the Inventory File Name.
inv.txt
File Loaded.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Lab7Test2.main(Lab7Test2.java:74)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.