[英]Java InputMismatchException Errors
我正在嘗試為我的Java類編寫一個清單程序。 該程序需要從.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行:2號= scanFile.nextInt()。 僅當我將'count'和'number'變量設置為以上的* 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.
我發現您的實施中有一些小錯誤。 我嘗試用自己的列表進行嘗試,盡管我不確定是否適合您的格式。 這是我的清單:
2
1000
2
2
1001
1
3
我對您的代碼做了一些修改,但是樣式可以輕松地更好。 但是您可以隨后執行此操作。 我注釋掉了部分實現,以便您可以輕松看到更改。
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;
//while (countAt < (count * 3)) {
while (countAt < (count * 3)) {
//if(number == 0) // Number 0 = partNumb[]
if (countAt%3 == 0) // Number 0 = partNumb[]
{
number = scanFile.nextInt();
partNumb[index] = number;
index++;
//number++;
countAt++;
//else if(number == 1) //Number 1 = qoh[]
} else if (countAt%3 == 1) // Number 1 = qoh[]
{
number = scanFile.nextInt();
qoh[index2] = number;
index2++;
//number++;
countAt++;
//else if(number == 2) //Number 2 = reorder[]
} else if (countAt%3 == 2) // Number 2 = reorder[]
{
number = 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))
while (countAt < (count)) {
System.out.println(partNumb[index]); // Print All Values in the
// partNumb[] Array.
index++;
countAt++;
}
// ----END PROGRAM
// -----//////////////////////////////////////////////////////////////
}
如果我的文件正確,測試將顯示正確的輸出:
Enter the Inventory File Name.
C:\Users\Markus\Desktop\test.txt
File Loaded.
Data Loaded to Arrays
1000
1001
希望這對您的實施有所幫助。
真誠的
最高
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.