我的阵列打印为NULL

时间:2017-06-29 14:55:40

标签: java arrays null

在方法中,我有所有这些初始化

Scanner input = new Scanner(System.in);
    File file = new File("order.dat");
    File viewOrder = new File("ViewOrder.dat");

    String orderNo, itemNo, itemNameHolder, qtyHolder, priceHolder, status;
    int hold, count = 0, countArray = 0;
    double tempPriceHolder, totalPrice = 0;
    String tempStatus = "";

    String[] holdItemNo = null;
    String[] holdName = null;
    Integer[] holdQty = null;
    Double[] holdTotal = null;
    String[] holdStatus = null;

之后,我尝试读取文件中的所有内容并将内容存储到holdX数组

        try {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

        String line = null;

        while ((line = br.readLine()) != null) {
            String tokens[] = line.split(";");

            orderNo = tokens[0];
            itemNo = tokens[1];
            itemNameHolder = tokens[2];
            qtyHolder = tokens[3];
            priceHolder = tokens[4];
            status = tokens[5];

            if (orderNo.equalsIgnoreCase(userOrderNo)) {

                tempPriceHolder = Double.parseDouble(priceHolder);
                hold = Integer.parseInt(qtyHolder);
                tempPriceHolder = tempPriceHolder * hold;
                totalPrice += tempPriceHolder;

                countArray++;
                holdItemNo = new String[countArray];
                holdName = new String[countArray];
                holdQty = new Integer[countArray];
                holdTotal = new Double[countArray];
                holdStatus = new String[countArray];                

                if (status.matches("s")) {

                    tempStatus = "Success";
                } else if (status.matches("p")) {
                    tempStatus = "Partially Full";
                } else if (status.matches("o")) {
                    tempStatus = "Out of Stock";
                }

                holdItemNo[count] = itemNo;
                holdName[count] = itemNameHolder;
                holdQty[count] = hold;
                holdTotal[count] = tempPriceHolder;
                holdStatus[count] = tempStatus;

                count++;
            }
        }

    } catch (Exception e) {
        System.out.println("Error");
    }

最后,我将我的所有数组写入一个新文件。

        System.out.printf("%s %15s %15s %10s %10s\n", "Item No", "Description", "Quantity", "Total", "Status");

    for (int i = 0; i < holdItemNo.length; i++) {            
        System.out.printf("\n%-11s %-18s %-13s $%-8s %s \n", holdItemNo[i], holdName[i], holdQty[i], holdTotal[i], holdStatus[i]);           
    }
    System.out.println("-----------------------------------------------------------------------");
    System.out.printf("%46s %s\n", "$", totalPrice);

    System.out.print("Print Order to file Y/N: ");
    String choice = input.next();

    if (choice.equalsIgnoreCase("y")) {
        try {
            PrintWriter bw = new PrintWriter(new FileWriter("ViewOrder.dat", true));

            for (int i = 0; i < holdItemNo.length; i++) {
                bw.write(userOrderNo + ";" + holdItemNo[i] + ";" + holdName[i] + ";" + holdQty[i] + ";" + holdTotal[i] + ";" + holdStatus[i] + "\n");
                bw.flush();
            }

            bw.flush();
            bw.close();

            System.out.println("Sucessfull!");
        } catch (Exception e) {
            System.out.println("Error");
        }

    } else if (choice.equalsIgnoreCase("n")) {

        System.out.println("");
    }

但问题是我的代码工作正常,但输出不是我的预期。它打印出打印出的最后一个内容,并且子价格也正常工作,但其余的只打印出NULL。 Example

另外,它给了我一个关于在array.length上解除可能的空指针的警告

for (int i = 0; i < holdItemNo.length; i++) {
                bw.write(userOrderNo + ";" + holdItemNo[i] + ";" + holdName[i] + ";" + holdQty[i] + ";" + holdTotal[i] + ";" + holdStatus[i] + "\n");
                bw.flush();
           } 

1 个答案:

答案 0 :(得分:1)

猜测:

holdItemNo = new String[countArray];

以及以下几行:您在的读取循环(条件内)中创建数组对象

所以可能这种情况永远不会成真;因此你的数组保持全部为空。但即使满足条件 - 你可能希望发生更多然后一次。猜猜看:你正在创建完全 new 数组。当丢弃之前创建的数组时。每次if条件变为true时,您将丢失先前存储的值!

所以答案是:在进入循环之前创建数组。这意味着您要么必须先查询“要创建多少个插槽”;或者你必须创建一个说100个空槽的数组;然后在你的循环中你必须检查你是否还有空位。

或者你开始使用java.util.List resp。 ArrayList - 允许动态添加元素。