Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在文件中仅读取双值_Java - Fatal编程技术网

Java 如何在文件中仅读取双值

Java 如何在文件中仅读取双值,java,Java,我有一个这样的文件 07/15/14: 19.33 07/15/14: 13.14 07/15/14: 21.20 07/15/14: 22.67 public double totalHourlyOverall() throws FileNotFoundException { Scanner fileScanner=new Scanner(fos); while(fileScanner.hasNextLine()) { String s

我有一个这样的文件

07/15/14: 19.33

07/15/14: 13.14

07/15/14: 21.20

07/15/14: 22.67
public double totalHourlyOverall() throws FileNotFoundException

{
    Scanner fileScanner=new Scanner(fos);
    while(fileScanner.hasNextLine())
    {
            String str=fileScanner.nextLine();
            String[] array=str.split(":");
            total+=Double.parseDouble(array[1]);
            counter+=1;
    }
    fileScanner.close();
    return overallHourly=total/counter;
}
我需要一种方法来读取这些双值并打印出平均值。这就是数字的保存方式

public void hourlyOverall() throws IOException
{
    //construct the date to print into a file
    java.sql.Date date=new java.sql.Date(time);
    //set hourlyOverall variable
    hourlyOverall=tips/hours+hourlyWage;
    //construct the printwriter with a file writer in it.  
    //set the FileWriter append parameter too true so that the 
    //info is added onto the end of the file, and the file is
    //not overwritten
    PrintWriter editFile=new PrintWriter(new FileWriter("wage info",true));
    editFile.printf("%tD: %.2f \n ", date, hourlyOverall);
    editFile.close();
}
这就是我试图阅读它们的方式

public double totalHourlyOverall() throws FileNotFoundException
{
    Scanner fileScanner=new Scanner(fos);
    while(fileScanner.hasNextDouble())
    {
            total+=fileScanner.nextDouble();
            counter+=1;
    }
    fileScanner.close();
    return overallHourly=total/counter;
}
不过,它没有拿到双打。我做错了什么

编辑:

所以我现在得到一个数组越界异常。我的代码是这样的

07/15/14: 19.33

07/15/14: 13.14

07/15/14: 21.20

07/15/14: 22.67
public double totalHourlyOverall() throws FileNotFoundException

{
    Scanner fileScanner=new Scanner(fos);
    while(fileScanner.hasNextLine())
    {
            String str=fileScanner.nextLine();
            String[] array=str.split(":");
            total+=Double.parseDouble(array[1]);
            counter+=1;
    }
    fileScanner.close();
    return overallHourly=total/counter;
}

应该有一个数组[1],因为每一行被分成两行,对吗?

我认为你应该逐行阅读,然后跳过10个字符,一直读到行的末尾。

有一种更简单的方法:读入每一行,然后在行中的一个公共标记上拆分

我不会写出所有代码,但这里有一种方法可以阅读整行代码:

while(fileScanner.hasNextLine()) {
    String line = fileScanner.nextLine();
    String[] lineFragments = line.split(":");
    // the date is in lineFragments[0]; the number is in lineFragments[1].
    // I leave the rest as an exercise to the reader.
}

hasNextDouble将为“07/15/14”返回false,您将立即停止阅读。

另一种方法是使用
模式和
匹配器

Pattern p = Pattern.compile("(\\b[-\\d\\.]+\\b)");   // pattern to match any double value (+/-) with boundaries
Matcher m;
while(fileScanner.hasNextLine()) {
    String line = fileScanner.nextLine();
    m = p.matcher(line); // try to match the regular expression
    if ( m.find() ) {
        String value = m.group(1); // get the match in group 1
        // turn to a double, etc...
    }
}

为什么,哦,为什么,您使用的是
java.sql.Date
?数组索引超出范围=您需要跳过空行。您可以通过检查结果数组的长度是否小于2来执行此操作。因为java.sql.Date执行了我希望执行的操作。为什么我不在这里使用它呢?我想你应该在
上拆分:“
Double.parseDouble()
可以加上空格。删除空白字符。新的Double(String)或BigDecimal如何?我看不到在OP的代码中使用
Double.parseDouble()
。@ScaryWombat:这就是我推动OP前进的方向。