Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Reading file of floats into array list and sorting into subsets

1. Reading file of floats into array list and sorting into subsets

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())
{
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.

2. Re: Reading file of floats into array list and sorting into subsets

We don't read minds. Do you think that posting the exact error message might be helpful?

3. Re: Reading file of floats into array list and sorting into subsets

Originally Posted by Junky