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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

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

    Quote Originally Posted by Junky View Post
    We don't read minds.
    Speak for yourself... at the moment, I'm sensing that you're tired and grumpy!
    And believe me, that was no party trick, It's just something you pick up from a year of JavaPenguin
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Reading from a sequential file to an array
    By Xrrak in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: August 20th, 2011, 01:33 PM
  2. loading things from a text file into an array list
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2011, 03:32 PM
  3. Sorting a doubly linked list
    By snufkin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2010, 12:01 PM
  4. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM
  5. Reading a file, then putting it in an array
    By Gondee in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2010, 11:11 AM

Tags for this Thread