Note that there are some explanatory texts on larger screens.

plurals
  1. POReading from file and sort into subsets
    primarykey
    data
    text
    <p>I am working on a project where I need to read a bunch of floating point integers from a file and sort them into subsets. The subsets need to be sorted by their distance apart. I need to first pull the first element out of the file and use that as the distance comparison d. Then take the next element a and put it into a separate list to be compared, then read the third element b and compare it with a. b needs to be at least d apart from a, if so it would be put into the subset with a. Then b would equal a and you would move on down till the end and make those a set, then you would start over and do it again until there are no more elements. </p> <p>I am trying to do this by using array lists, all I have so far is reading the file and putting it into an array list then sorting it. I need some help with pulling the first element out and then making the comparisons.</p> <pre><code>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&lt;Float&gt; list = new ArrayList&lt;Float&gt;(); 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&lt;Float&gt; sorted = new ArrayList&lt;Float&gt;(); while (file.hasNext()) { Float val = file.nextFloat(); int i = sorted.size(); float temp = sorted.get(i-1); while (i &gt; 0 &amp;&amp; temp &gt; val) { i--; temp = sorted.get(i-1); } sorted.insert(i, val); } for (Float i: sorted) System.out.println(i); } } </code></pre> <p>I added in the new method, but I am still having problems getting it to work I am getting the errors</p> <p>FloatFromFile.java:50: error: cannot find symbol sorted.insert(i,val); ^ symbol: method insert(int,Float) location: variable sorted of type ArrayList 1 error </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload