简体   繁体   中英

Determining the Min and Max Double Values From a File

I am reading Earth quake Stats from a file and I need to be able to determine the Min and max values of the Magnitudes. there are about 831 magnitudes. I tried to create a local double max = Double.MAX_VALUE; double min = Double.MIN_VALUE; double max = Double.MAX_VALUE; double min = Double.MIN_VALUE; variables and compare those to the double magnitude values I am pulling from the file, but when I return the value, it just gives me the lowest and highest values for any double value. Here is my code so far.

Data from file example:

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);


        }

    }

Results:

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

Try changing this:

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

to this:

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

After all, if you start off thinking you've already seen MAX_VALUE as the highest, you're never going to see anything greater than that, are you?

EDIT: Note the use of -Double.MAX_VALUE here, instead of Double.MIN_VALUE . MIN_VALUE is the smallest positive number, whereas I assume you really want "the most strongly negative finite value".

You want the first value you read to replace both max and min . An alternative would be to use Double instead, to represent the "missing" value with null to start with:

Double max = null;
Double min = null;

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

As a style issue, I'd also only compute the average after you've read the whole of the input - you're repeatedly overwriting the value for no reason. The only reason not to do this is to avoid having to think about the case where count is 0 (ie an empty file) but I'd personally handle that separately anyway - when you have no values, there simply isn't an average.

dont use Double.MIN_VALUE its not what you expect it to be

Double.MIN_VALUE its not a negative number Double.MIN_VALUE is a very small nummber its 4.9e-324; // 0x0.0000000000001P-1022

Comparing with it will break as soon as you use negative numbers

A small utility class i wrote to help with it:

public class MinMaxUtil<T extends Comparable<T>>
{
  private T minimum;
  private T maximum;

  public MinMaxUtil()
  {
    reset();
  }

  public void check( T value)
  {
    if ( minimum == null || value.compareTo(minimum) < 0  )
      minimum = value;
    if ( maximum == null || value.compareTo(maximum) > 0 )
      maximum = value;
  }

  public void check( T [] values )
  {
    for (T value : values)
      {
      check(value);
      }
  }

  public void check( Collection<T> values )
  {
    for (T value : values)
      {
      check(value);
      }
  }

  public T getMinimum()
  {
    return minimum;
  }

  public T getMaximum()
  {
    return maximum;
  }

  public void reset()
  {
    minimum = null;
    maximum = null;
  }

  @Override
  public String toString()
  {
    return "MinMaxUtil{"+
        "minimum="+minimum+
        ", maximum="+maximum+
        '}';
  }
}

Change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM