0

Problem solved

I would like to find the highest and lowest value from a file. I have converted the figures in a file from String to double. How can I do if I would like to use Get method to find the highest and lowest amount?

  • Are you quite sure your `avg_weekly` should only have 3 weeks in it? – M. Prokhorov Nov 09 '17 at 12:19
  • You also didn't say which "highest" and which "lowest" you want. Is it highest sale in a week? Is it highest among all weeks? It it highest weekly total? All of those? – M. Prokhorov Nov 09 '17 at 12:25
  • Possible dupe: https://stackoverflow.com/questions/26793926/how-to-find-the-max-and-min-values-in-an-arraylist. Another one for array: https://stackoverflow.com/questions/13547618/java-find-high-and-low-number-in-an-array and another one for list: https://stackoverflow.com/questions/8304767/how-to-get-maximum-value-from-the-list-arraylist (just _max_ there, but _min_ is similar). – Tom Nov 09 '17 at 12:31

5 Answers5

1

Edit: I complete made a mess of the original answer, as I didn't realise Strings would be compared lexicographically by using the standard Collections() class methods.

So the best way to do this using Collections() is to create a custom Comparator

Comparator<String> sort = (o1, o2) -> Double.valueOf(o1).compareTo(Double.valueOf(o2));
String max = Collections.max(numList, sort);
String min = Collections.min(numList, sort);

Try it online!

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • 1
    That's better. You can also use a method reference for the comparator. Something like `Comparator sort = Comparator.comparing(Double::valueOf);`. – Tom Nov 09 '17 at 15:05
1

you should put data into arraylist while reading from the file. and then start sorting the numbers using merge or quick.

i'm using inbuild Collections.sort() method.

package com.collections.java.basic;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortingDataWhileReading {
    public static void main(String[] args) throws IOException {
          BufferedReader br = new BufferedReader(new FileReader("E:\\BUILD\\numbers.txt"));//this file contains several double data.
            List<Double> numbers = new ArrayList<Double>();
            String line = null;

             //String line = br.readLine();

             while ((line = br.readLine()) != null) {
                 String []strNumbers = line.split(" ");
                 for(String strNumber : strNumbers){
                     numbers.add((double) Double.parseDouble(strNumber));
                 }

             }   

             br.close();

             Collections.sort(numbers);

             System.out.println("minimum value" + numbers.get(0));
             System.out.println("minimum value" + numbers.get(numbers.size() - 1));

             System.out.println(numbers);
    }
}

This should solve your problem.

Jabongg
  • 2,099
  • 2
  • 15
  • 32
0
Double higher = 0;
Double lower = 0;
for (String item:numList)
        { 
            Double result = Double.parseDouble(item);
            if (result>higher) { higher = result;}
            if (result<lower) { lower = result;}
            //Calculate the total sales for each week
            total += result;
        }
localghost
  • 419
  • 2
  • 6
0
public static double getMin(List<String> list) {
    double min = Double.MAX_VALUE;
    for (String s : list) {
        double d = Double.valueOf(s);
        min = min > d ? d : min;
    }
    return min;
}

public static double getMax(List<String> list) {
    double max = Double.MIN_VALUE;

    for (String s : list) {
        double d = Double.valueOf(s);
        max = max < d ? d : max;

    }
    return max;
}

Java8 example:

public static double getMinJ8(List<String> list) {
    return list.stream().map(s -> Double.valueOf(s)).min((d1, d2) -> d1.compareTo(d2)).get();
}
public static double getMaxJ8(List<String> list) {
    return list.stream().map(s -> Double.valueOf(s)).max((d1, d2) -> d1.compareTo(d2)).get();
}
0

store all the values in a treeset so all the elements will be sorted. Now use first and last

sample code

    TreeSet s=new TreeSet();
    //add elements to the set
    s.add(100);
    s.add(200);
    s.add(1);
   // use first() method to get the lowest
    System.out.println(s.first());
   //use last method to get the highest
    System.out.println(s.last());

output: 1

200

SpringLearner
  • 13,738
  • 20
  • 78
  • 116