我有一个文本文件,其中包含格式为 00:00 的数字条目。我知道如何从文件中读取这个字符串。我不知道的是如何解析它,以便小数点的左侧知道与右侧作为一个数字连接。
如果我在一个拆分中进行拆分,当我只需要一个值时,内部拆分会为我提供两个值。
File database = new File (FILE);
Scanner read = new Scanner (database);
String [] dataEntry;
String [] times;
float [] correctTime = null;
while (read.hasNext ())
{
dataEntry = read.nextLine().split(",");
times = dataEntry[0].split(":");
correctTime = new double[times.length];
//I get stuck here, I know the above line is incorrect
}
最佳答案
我猜你想拥有一个替身来进行工作时间管理之类的事情。
// 06:30 -> 6.5
// 07:45 -> 7.75
String[] hhmm = dataEntry[0].split(":");
int hh = Integer.parseInt(hhmm[0]);
int mm = Integer.parseInt(hhmm[1]);
double decimalTime = hh + mm / 60.0; // Floating point division because of 60.0
或者可以使用新的 java time API。
关于java - 从 .txt 文件读取后从 00 :00 format to a double 00. 00 格式转换字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56156839/