首页 > 解决方案 > 为什么当我尝试访问我的字符串数组时会得到“null”?

问题描述

我只是想访问我通过这样做填充的数组(items [j]):

                            for(int j = 0;j<items.length;j++){
                            for(int i = 0;i<prices.length;i++){
                                items[j] = x.next();
                                System.out.print(items[j] + ", ");

                                prices[i] = c.nextDouble();
                                System.out.printf("$%.2f \n", prices[i]);

上面的代码按预期工作,但是当我尝试在 for 循环之外访问数组(items[j])时会出现“空”问题。 像这样:

    System.out.printf("%s, ", items[5]); //accessing 6th item on the menu(Coffee)
    System.out.printf("$%.2f", prices[5]); //accessing 6th items price.(2.59)


我的输出:

null, $2.59

它似乎适用于 Prices 数组,但不适用于 Items 数组。


我的预期输出:

Coffee, $2.59



如果有任何混淆,这是我的完整代码:

            int size = 1000;
            String [] items =  new String[10];
            double [] prices = new double[size];
                System.out.println(" ");
                    File fileText = new File("inventory.txt");
                    Scanner x = new Scanner(fileText);
                    x.useDelimiter("[^A-Za-z]+"  + "[^A-Z a-z]+");

                    Scanner c = new Scanner(fileText);
                    c.useDelimiter("[^0-99.99]+");
                    try{

                        System.out.println("The items in inventory.txt are: ");

                        while(x.hasNext()&&c.hasNext()){
                            for(int j=0;j<items.length;j++){
                                for(int i = 0;i<prices.length;i++){
                                    items[j] = x.next();
                                    System.out.print(items[j] + ", ");

                                    prices[i] = c.nextDouble();
                                    System.out.printf("$%.2f \n", prices[i]);
                                }
                            }
                        }
                    x.close();
                    c.close();
                    }   
        catch(NoSuchElementException g){
            System.out.println(" ");
        }

        System.out.printf("%s, ", items[5]); //accessing 6th item on the menu(Coffee)
        System.out.printf("$%.2f", prices[5]); //accessing 6th items price.(2.59)



我的程序正在读取的文本文件(inventory.txt):

Espresso, 2.10

Mochaccino, 5.99

Irish Coffee, 7.99

Caffe Americano, 6.40

Latte, 1.99

Coffee, 2.59

Cappuccino, 3.50

blackCoffee, 8.90

whiteCoffee, 5.54

yellowCoffee, 4.80

greenCoffee, 6.12

purpleCoffee, 3.54

标签: java

解决方案


尝试这个。我避免使用分隔符,因为它们不是必需的,并且我删除了一个无用的 for 循环,这在MarsAtomic的评论中也有争议......

像这样嵌套你的 for 循环的目的是什么?数据文件并不表明每个项目都有多个价格......您可以(并且应该)在单个循环的每次迭代中读取一整行数据。试试看,然后看看你的问题是否会神奇地消失。

public static void main(String[] args) throws FileNotFoundException {
    int size = 1000, commaIndex = 0;
    String[] items = new String[10];
    double[] prices = new double[size];

    File fileText = new File("inventory.txt");
    Scanner c = new Scanner(fileText);
    try {
        System.out.println("The items in inventory.txt are: ");

        while (c.hasNextLine()) {
            for (int j = 0; j < items.length; j++) {
                String line = c.nextLine();
                // Empty line is scanned so i need to decrease j
                // in order to have the right indexes for items and prices
                if (line.isEmpty())
                    j--;
                commaIndex = line.indexOf(",");
                items[j] = line.substring(0, commaIndex);
                prices[j] = Double.parseDouble(line.substring(commaIndex + 2));  
            }
        }
        c.close();
    } catch (NoSuchElementException g) {
        System.out.println(" ");
    }

    System.out.printf("%s, ", items[5]); //accessing 6th item on the menu(Coffee)
    System.out.printf("$%.2f", prices[5]); //accessing 6th items price.(2.59)

}

推荐阅读