java - 从文件中确定最小和最大 double 值

标签 java file-io

我正在从文件中读取地震统计数据,我需要能够确定震级的最小值和最大值。大约有 831 个星等。我试图创建一个本地 double max = Double.MAX_VALUE; double min = Double.MIN_VALUE; 变量并将它们与我从文件中提取的 double 幅度值进行比较,但是当我返回该值时,它只为我提供任何 double 值的最低和最高值。到目前为止,这是我的代码。

来自文件示例的数据:

1.6,"Southern California","Wednesday, January 18, 2012 19:19:12 UTC"
1.8,"Southern California","Wednesday, January 18, 2012 19:03:00 UTC"
1.8,"Southern California","Wednesday, January 18, 2012 18:46:53 UTC"
4.7,"Bonin Islands, Japan region","Wednesday, January 18, 2012 18:20:40 UTC"
1.6,"Southern California","Wednesday, January 18, 2012 17:58:07 UTC"
1.0,"Northern California","Wednesday, January 18, 2012 17:48:03 UTC"
5.2,"Santa Cruz Islands","Wednesday, January 18, 2012 17:26:02 UTC"
import java.util.*;
import java.io.*;

public class QuakeStates2 
{       

    public static void main(String[] args) throws IOException
        {       
            double count = 0.0;
            double mag = 0.0;
            double total = 0.0;
            double average = 0.0;
            double max = Double.MAX_VALUE;
            double min = Double.MIN_VALUE;
            String area = null;
            String date = null;


            Scanner keyboard = new Scanner(System.in); //Setup the Keyboard scanner
            System.out.print("Enter the filename: "); // User input for the filename

            String filename = keyboard.nextLine();  //Scanner stores the file name as a String Value

            File file = new File(filename);         //File turns the Scanner input into a file
            Scanner inputFile = new Scanner(file);  //inputFile holds the file info and Reads up to the comma

            while (inputFile.hasNextLine()) 
            {
                String line = inputFile.nextLine();
                count++;
                StringTokenizer str = new StringTokenizer(line);

                if (str.hasMoreTokens())
                    {
                    mag = Double.parseDouble(str.nextToken(",")); 
                    area = str.nextToken();
                    date = str.nextToken("\\w");
                    //System.out.println(mag);
                    //System.out.println(area);
                    //System.out.println(date);
                    }
                if ( mag > max)
                {
                    max = mag;
                }

                if ( mag < min)
                {
                    min = mag;
                }
                total = mag+total;
                average = total/count;

            }

            inputFile.close();

            System.out.println("# of Lines in the file: " + count);
            System.out.println("Sum of Magnitudes: " + total);
            System.out.println("Average Magnitude: " + average);
            System.out.println("Max Magnitude: " + max);
            System.out.println("Min Magnitude: " + min);


        }

    }

结果:

Enter the filename: C:\Users\Owner\Desktop\workspace\QuakeStatistics\quakes1.2012.txt
# of Lines in the file: 821.0
Sum of Magnitudes: 1747.0000000000007
Average Magnitude: 2.127892813641901
Max Magnitude: 1.7976931348623157E308
Min Magnitude: 4.9E-324

最佳答案

尝试改变这个:

double max = Double.MAX_VALUE;
double min = Double.MIN_VALUE;

为此:

double max = -Double.MAX_VALUE;
double min = Double.MAX_VALUE;

毕竟,如果您一开始就认为您已经看到 MAX_VALUE 是最高的,那么您永远不会看到比这更高的,对吗?

编辑:注意这里使用的是 -Double.MAX_VALUE,而不是 Double.MIN_VALUEMIN_VALUE 是最小的正数,而我假设您真的想要“最负的有限值”。

您希望读取的第一个值替换 maxmin。另一种方法是使用 Double 来表示以 null 开头的“缺失”值:

Double max = null;
Double min = null;

if (max == null || mag > max)
{
    max = mag;
}
// Ditto for min

作为一个风格问题,我还在您读取整个输入后计算平均值 - 您无缘无故地重复覆盖该值。不这样做的唯一原因是避免考虑 count 为 0 的情况(即空文件),但无论如何我个人都会单独处理 - 当你没有值时,那里根本不是平均值。

关于java - 从文件中确定最小和最大 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999062/

相关文章:

java - ClientAbortException : java.net.SocketException:jboss 服务器上间歇性出现连接重置错误

Javascript - 为什么我有时无法使用 GDownloadUrl 读取文件内容?

c++ - 每次运行都从不同的文件中读取整数,为什么?

c - 将文本文件读入二维数组

javascript - Nodejs 文件流并发

当使用节点(*)副节点(1,2,3...)时,Java Neo4j Cypher 查询会导致 unsupportedException

java - 寻找特定的正整数

java - Jboss 和 Spring 中的 Logback 经典

java - 是否可以在 Java 中使用正则表达式在 + 和 - 之间切换?

java - 有谁知道一些返回操作系统友好文件名的 Java 类?