1

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?

Razmon
  • 61
  • 6
  • You're doing string comparisons. "9" is greater than "3456" when doing a string comparison. "-9" would be less than "-1234". Use Integer.parseInt(String s) to convert the string into an int, then you can do integer comparisons. – NomadMaker May 08 '20 at 23:17

4 Answers4

1

Compact solution with comments and explanations:

public static void main(String[] args) {

    // Create an input stream
    try (Stream<String> stream = Files.lines(Paths.get("average.dat"))) {

        // Get each line from the stream
        stream.forEach(line -> {

            // Cut the line into peaces
            String[] numbers = line.split(" ");

            // Create a list of integers from the pieces
            List<Integer> integers = Arrays.stream(numbers).map(Integer::parseInt).collect(Collectors.toList());

            // Calculate max
            System.out.println("Max:" + integers.stream().flatMapToInt(IntStream::of).max().orElseThrow());

            // Calculate min
            System.out.println("Min:" + integers.stream().flatMapToInt(IntStream::of).min().orElseThrow());

            // Ouput a new line
            System.out.println();
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
1

Actually, you don't need to compute the max and min separately. Consider the following:

  • set the max and min to the first value.
  • Check to see if the value is a max, if so, assign it to max.
  • other wise, just compare to min and assign as appropriate.
while (file.hasNextLine()) {
    // Storing the ints from the data file into an array.
    String[] str = file.nextLine().split("\\s+");
    int max = Integer.parseInt(str[0]);
    int min = max;
    for (int i = 1; i < str.length; i++) {
        int v = Integer.parseInt(str[i]);
        if (v > max) {
            max = v;
        } else if (v < min) {
            min = v;
        }
    }
    System.out.println("Max:" + max);
    System.out.println("Min:" + min);
    System.out.println();
}
WJS
  • 36,363
  • 4
  • 24
  • 39
0

You should use: Integer.pharseToInt() Then when you'll have an array of integers and you can find the max value easily:

int getMax(int [] array){
    int max = INTEGER.MIN_VALUE;
    for (int i=0: i<array.length; i++)
        if (array[i]>max)
            max = array[i];
return max;
}

Totally symetric function for finding min value.

0

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); 
     }
0xh3xa
  • 4,801
  • 2
  • 14
  • 28