I am trying to write an Inventory program for my Java class. This program needs to read initial Inventory data from a .txt
file (iterating a set amount of times - defined by the 1st integer in the inv.txt
file we are using). My text file is correct, Scanners, Arrays, and Loops seem to be correct, however I am getting an InputMismatchException.
The .txt file is formatted as follows:
XXXX - Count - # of times to iterate
XXXX - Product Code
XXXX -Quantity On Hand
XXXX - Reorder Level
Where the Product Code, QOH, and Reorder Level are repeated for each product, the COUNT is only in the file once.
All values must be read, and each stored in their own respective array.
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;
It is the beginning of the below loop that is throwing the exception, specifically 5 lines down: number 2 = scanFile.nextInt(). I ONLY get this error when i have the 'count' and 'number' variables set to * 3 as they are above (to ensure that each PRODUCT has 3 VALUES: PRODUCT CODE, QUANTITY ON HAND, REORDER LEVEL).
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 -----//////////////////////////////////////////////////////////////
}
}
I have tried countless ways of correcting this based on at least 3 days of researching the issue, and so far have not been able to come up with any way to fix this InputMismatchException error?. If anyone has any suggestions, please let me know, Thank you.
Stack Trace:
----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.