小编use*_*347的帖子

在字符串上使用line.split时获取ArrayIndexOutOfBound

获取indexOutOfBound.成功保存文件后.我打开文件来执行以下任务:找到total costPerItem,totalCount和sum.不知道我为什么离开了界限.错误指出

totalCost =+ Double.parseDouble(index[1].trim());
Run Code Online (Sandbox Code Playgroud)

源代码:

 Scanner data = new Scanner(new File("registryCopy.txt"));

    while(data.hasNext()){
        String line = data.nextLine();
        System.out.println(line);

        String[] index = line.split("\\t");
        totalCost += Double.parseDouble(index[1].trim());
        totalCount += Integer.parseInt(index[2].trim());

        sum = totalCost/totalCount;

    }
Run Code Online (Sandbox Code Playgroud)

错误

Error: "main" java.lang.ArrayIndexOutOfBoundsException: 1 
Run Code Online (Sandbox Code Playgroud)

这是: totalCost += Double.parseDouble(index[1].trim());

由于某些原因,它不分裂线.文本文件如下:

item1 15.00 3
item2 15 2
item3 14 3
Run Code Online (Sandbox Code Playgroud)

文件是在创建之前创建的,并且要求用户输入该数据.来源:

while(userInput.charAt(0)!='n'){
        /**
         * Need only one PrintWriter out0 i.e, then I either use the userbased name OR fixed name creation i.e registryCopy.txt(true will
         * not overwrite the file …
Run Code Online (Sandbox Code Playgroud)

java io file-io

2
推荐指数
1
解决办法
62
查看次数

标签 统计

file-io ×1

io ×1

java ×1