如何解决为什么会发生? :线程“ main” java.lang
我写了这段代码来计算在某种产品上花费的金额。 我不知道为什么,但是我写的代码给我一个错误“线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException”。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class test7 {
public static void main(String[] args) throws FileNotFoundException {
String section = "butchery";
totalGastoNaSeccao(section);
}
public static void totalGastoNaSeccao(String section) throws FileNotFoundException
{
Scanner in = new Scanner(new File("data.txt"));
String line;
double total = 0;
while (in.hasNextLine())
{
line= in.nextLine();
String [] itensDaLinha= line.split(" ");
if (section.equals(itensDaLinha[1]))
{
total += Double.parseDouble(itensDaLinha[4]);
}
}
in.close();
System.out.println("section[" + section+ "] total=" + total);
}
}
这是.txt文件
item section product quantity value
1 fruits orange 1.5 1.3
2 fruits apple 2.15 4.35
3 fruits melon 3.45 2.2
4 butchery bife 0.7 12.5
5 butchery frango 0.75 5.0
6 drinks water 6 3.0
7 drinks cocacola 2 2.30
您能帮我找出错误原因吗?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。