0

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.
Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118
  • 5
    Is just me or is this code kind of hard to read? – But I'm Not A Wrapper Class Aug 01 '14 at 17:53
  • which line gives you the error? give more info about your error – Kick Buttowski Aug 01 '14 at 17:53
  • 1
    Post the full stack trace and the line throwing the exception. Also, why the unnecessary imports? – Unihedron Aug 01 '14 at 17:55
  • Code is hard to read because I'm still getting the hang of how to format it correctly, I apologize for that. I'll post the stack trace following this comment, and it is line 74 throwing the error. The Data format unfortunately is "as-is", generated by the instructor. And i'll check to see if I've advanced the Scanner. Stack Trace will follow this code, and Thank you for the prompt responses. – Keith Kornacki Aug 01 '14 at 18:05
  • And as for the unnecessary imports, they are going to be used later in the program, unfortunately I have not gotten that far yet. -->peeSkillet, I was under the assumption that by using scanFile.nextInt() I was advancing to the next Line, is that incorrect? – Keith Kornacki Aug 01 '14 at 18:12
  • if number = 3 then it means there are 3 products and why you are multiply number * 3 to make it 9 products and then again multiplying it with 3 after assigning to count that makes the while condition `countAt < 27`. is that you are aiming at? – Sanjeev Aug 01 '14 at 18:14
  • number = scanFile.nextInt(), which in my .txt file is 8. Therefore number = 8. Multiplied by 3 because each product has 3 parts: Product Code, Quant. On Hand, and Reorder Level. This way the loop iterates 3 times for each product. Total iterations (count) = 8 * 3 --> 24. So the program knows I have 24 lines to be read and processed. – Keith Kornacki Aug 01 '14 at 18:18
  • I would just read line by line. For each iteration, `String line = scanner.nextLine()` and parse it `int value = Integer.parseInt(line.trim());` Do that three times each iteration. I never use `nextXxx` if I ever do use Scanner. I always read line by line and split/parse if necessary. The number one reason I see InputMismatchException from newbie posts using Scanner could be solved by readding line by line instead of using `nextXxx` – Paul Samsotha Aug 01 '14 at 18:19
  • Again, I apologize for the lack of legibility. I was instructed to use "lots of comments" by our instructor, otherwise I'd use less, and space the code out more. – Keith Kornacki Aug 01 '14 at 18:21
  • And use a `for` loop instead of a while loop. You already know the count. `while` loops are more for when you don't know the number of iterations and there is some stopping condition – Paul Samsotha Aug 01 '14 at 18:21
  • peeSkillet, Thank you. I'll edit my code with you're suggestions, try to make it more legible, and if I still have an error, then I'll post the edited code. – Keith Kornacki Aug 01 '14 at 18:23
  • `number = scanFile.nextInt(); number *= 3;` till here number = 24 then you have `count = number;` so count is 24 and in while loop (countAt < (count * 3)) that makes (countAt < (24 *3)) – Sanjeev Aug 01 '14 at 18:23
  • I see what you mean, I didn't catch that the first few times I read over the code. Thank's Sanjeev, correcting that right now as well. – Keith Kornacki Aug 01 '14 at 18:27

1 Answers1

0

i have found some little errors in your implementation. I tried it with my own list, although i'm not totally sure, if it fulfits your Format. Here's my list:

2
1000
2  
2
1001
1
3

I adapted your code a little bit, but the style can easily be better. But you can do this afterwards. I commented out parts of your implementation, so that you can easily see the change.

    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
    // -----//////////////////////////////////////////////////////////////
}

The test shows the correct output, if my file is correct:

Enter the Inventory File Name.
C:\Users\Markus\Desktop\test.txt
File Loaded.
Data Loaded to Arrays
1000
1001

I hope this will help you with your implementation.

Sincerely,

Max

cyr_mwe
  • 11
  • 1
  • Max, that version works perfectly, Thank you! However, I still don't quite *understand* what I did wrong in mine. I see you used countAt% = 0, 1 and 2, and that you didn't use number *= 3, but rather you determined that value in the loop. Are those the 2 main factors that could have caused my error? – Keith Kornacki Aug 01 '14 at 18:52
  • Ok at first, the number *= 3: you don't need it, because number determines the length of the three arrays. So for each array you only need as much space, as you have number of Products. In my example i have 2 Products, so the three arrays only need a length of 2 each. – cyr_mwe Aug 01 '14 at 19:01
  • Now we go to the countAt% = 0, 1 and 2: countAt iterates through the file. So for each Product you will have 3 numbers - the product code, the quantity on hand and the reorder level. countAt is kinda the count how many numbers you have read. The format in the list say that every third number from the beginning contains the next product code.(countAt%3==0). Every third number beginning from the second number will be the a Qoh and same goes for the reorder level. So you have to use the modulo-Operator. For example the product code of the second product is read, when the countAt is at 3. – cyr_mwe Aug 01 '14 at 19:09
  • That makes sense, so I was telling the program to set each Array Size to 24, (8 * 3), rather than telling the program to READ 24 Lines, Thanks =) Very Helpful! – Keith Kornacki Aug 01 '14 at 19:10
  • Modulo Operator, that's what I forgot. I was trying to do that initially (do *this* every 3 counts), but I had forgotten about that operator. Barely touched on it in class. – Keith Kornacki Aug 01 '14 at 19:11