0

我正在尝试为我的 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.
4

1 回答 1

0

我在您的实施中发现了一些小错误。我用我自己的列表尝试过,虽然我不完全确定它是否符合您的格式。这是我的清单:

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

我希望这对您的实施有所帮助。

真挚地,

最大限度

于 2014-08-01T18:36:07.627 回答