Hello, I am working on an assignment where I need to sort floats into subsets based on deviation. I need to pull the first element out and use it to compare the rest. a good way to think about it is d = first element and a = the first element in the remaining list, and b = the second element in the list. I need to put the floating points into subset where the elements are at least d apart
Example: d= 2 and list {5, 7, 8, 12, 4, 6, 7}
needs to be put into sets {5, 7, 12} {8, 4, 6} {7}
this is what I have so far
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.util.Collections; public class FloatFromFile { public static void main(String[] args) { Scanner file = null; ArrayList<Float> list = new ArrayList<Float>(); try { file = new Scanner(new File("test.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } while(file.hasNext()) { if (file.hasNextFloat()) list.add(file.nextFloat()); else file.next(); } // display unsorted list System.err.print("Origional List is:\n"); for (Float i: list) System.out.println(i); Collections.sort(list); // display sorted list System.err.print("Sorted List is:\n"); for (Float i: list) System.out.println(i); ArrayList<Float> sorted = new ArrayList<Float>(); while (file.hasNext()) { Float val = file.nextFloat(); int i = sorted.size(); float temp = sorted.get(i-1); while (i > 0 && temp > val) { i--; temp = sorted.get(i-1); } sorted.insert(i, val); } for (Float i: sorted) System.out.println(i); } }
I am getting one error with the sorted.insert(i, val), but I know that is not the only problem. Any help would be great.