javaarraysmaxmindata-files

finding max and min from a line of integers in a data file


I am trying to find the max and min of a line of text from a data file. Though there is plenty of lines of text in this data file.

DATA

-99 1 2 3 4 5 6 7 8 9 10 12345
10 9 8 7 6 5 4 3 2 1 -99
10 20 30 40 50 -11818 40 30 20 10
32767
255 255
9 10 -88 100 -555 1000
10 10 10 11 456
-111 1 2 3 9 11 20 30
9 8 7 6 5 4 3 2 0 -2 -989
12 15 18 21 23 1000
250 19 17 15 13 11 10 9 6 3 2 1 -455
9 10 -8 10000 -5000 1000

I have tried turning the lines into arrays and that has got me somewhere.

HERE IS MY CODE

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

// It is called "Average". Don't mind that.
public class Average 
{
    public static void main(String[] args) throws IOException
    {
        Scanner file = new Scanner(new File("average.dat"));


        String[] array = null;

        while(file.hasNextLine())
        {
        //Storing the ints from the data file into an array.
        String line = file.nextLine();
        String[] str = line.split("\\s+");
        array = new String[str.length];
        for (int i = 0; i < str.length; i++){
            array[i] = (str[i]);
        }
        System.out.println("Max:" + Average.getMax(array));
        System.out.println("Min:" + Average.getMin(array));
        System.out.println();
        }

    }
    //Simple way of getting max value. Nabbed it off google.
    public static String getMax(String[] inputArray)
    { 
        String maxValue = inputArray[0]; 
        for(int i=1;i < inputArray.length;i++){ 
          if(inputArray[i].compareTo(maxValue)>0){ 
             maxValue = inputArray[i]; 
          } 
        } 
        return maxValue; 
     }
    //Simple way of getting min value. Took off google as well.
    public static String getMin(String[] inputArray){ 
        String minValue = inputArray[0]; 
        for(int i=1;i<inputArray.length;i++){ 
          if(inputArray[i].compareTo(minValue)<0){ 
            minValue = inputArray[i]; 
          } 
        } 
        return minValue; 
      } 
}

HERE IS MY OUTPUT

Max:9
Min:-99

Max:9
Min:-99

Max:50
Min:-11818

Max:32767
Min:32767

Max:255
Min:255

Max:9
Min:-555

Max:456
Min:10

Max:9
Min:-111

Max:9
Min:-2

Max:23
Min:1000

Max:9
Min:-455

Max:9
Min:-5000

As you can see, the minimum value is correct. Though, the max value is not? It gives me 9 for no reason on some and I do not know why. You may ask "Why didn't you just make the array an int[] instead of a String[]?" I don't know how to copy the ints from the data file into an integer array. I only really know how to do it with a String array. Does anyone know the right way to do this?


Solution

  • You can compare using int

        public static String getMax(String[] inputArray) { 
            int maxValue = Integer.MIN_VALUE;
            for(int i = 0; i < inputArray.length; i++) { 
              if(maxValue < Integer.parseInt(inputArray[i])){ 
                 maxValue = Integer.parseInt(inputArray[i]); 
              } 
            } 
            return String.valueOf(maxValue); 
         }
    
        public static String getMin(String[] inputArray) { 
            int minValue = Integer.MAX_VALUE;
            for(int i = 0; i < inputArray.length; i++) { 
              if(minValue > Integer.parseInt(inputArray[i])){ 
                 minValue = Integer.parseInt(inputArray[i]); 
              } 
            } 
            return String.valueOf(minValue); 
         }