我正在创建一个程序,它将在txt文件上制作时间卡。它包含日期,时间和时间,并计算它以打印获得的金额和文件上的工作时间。我似乎无法让它工作,因为我在第35行有一个ArrayIndexOutOfBoundsException。这是我的控制台输出和我的代码如下。
在com.mangodev.Main.addMoney(Main.java:35)在com.mangodev.Main.main(Main.java:68)1:异常在线程 “主” java.lang.ArrayIndexOutOfBoundsException
public static void addMoney(String timeIN, String timeOUT, String Date) throws IOException {
String[] t1 = timeIN.split(":");
String[] t2 = timeOUT.split(":");
Integer i1 = Integer.parseInt(t1[0]);
Integer i2 = Integer.parseInt(t1[1]);
Integer i3 = Integer.parseInt(t2[0]);
Integer i4 = Integer.parseInt(t2[1]);
Integer[] t11 = new Integer[2];
Integer[] t12 = new Integer[2];
t11[0] = i1;
t11[1] = i2;
t12[0] = i3;
t12[1] = i4;
Integer min1 = (t11[0] * 60) + t11[1];
Integer min2 = (t12[0] * 60) + t12[1];
Integer wm = min2 - min1;
Integer ii1 = wm / 60;
String[] ii2 = ii1.toString().split(".");
Integer i5 = Integer.parseInt(ii2[1]);
i5 = i5 * 60;
String ii5 = i5.toString();
String tw = ii2[0] + ":" + ii5;
FileWriter fw = new FileWriter("TimeCard.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
out.println(Date + "---------------| Punch-In Time: " + timeIN + " | Punch-Out Time: " + timeOUT
+ "Time Worked: " + tw + "Money Earned: " + ii1 * 8 + " |-----------");
}
public static void addWeek(String week) {
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type the Punch-In time EX(03:11) or type \"new\" for a new week");
String Input1 = scanner.nextLine();
if (Input1 == "new") {
System.out.println("Type in the week date. EX(July 7-14)");
String week = scanner.nextLine();
addWeek(week);
} else {
System.out.println("Please type the Punch-Out time EX(03:11)");
String Input2 = scanner.nextLine();
System.out.println("Please type the Date EX(05/04/2022)");
String Input3 = scanner.nextLine();
addMoney(Input1, Input2, Input3);
}
}
}
发布于 2018-08-28 09:40:10
问题在于代码的这一部分:
Integer ii1 = wm / 60;
String[] ii2 = ii1.toString().split(".");
Integer i5 = Integer.parseInt(ii2[1]);
你在做Integer division然后试图在小数点上分裂。整数除法中永远不会有小数点(在整数除法中,余数被丢弃)
https://stackoverflow.com/questions/-100002439
复制相似问题