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: Finding biggest files, making list

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Finding biggest files, making list

    Okay, I know my issue is in my add function [adds to the array]
    so I'll only post that portion of the code
    	public void add(Comparable<Wrapper> c)
    	{
    		for(int x =0; x<CompFiles.length ; x++)
    		{
    			if(CompFiles[x] == null )
    			{
    				CompFiles[x] = c;
    				return;
    			}
    		}
    		for(int x = 0; x<CompFiles.length; x++)
    		{
    			int stat = c.compareTo((Wrapper) CompFiles[x]);
    			if(stat > 0)
    			{
    				CompFiles[x]=c;
    				return;
    			}
    		}
    	}
    Oh and the compareTo code
     
    	@Override
    	public int compareTo(Wrapper o)
    	{
    		return (int) (this.size - o.getSize());
    	}
    Which is in the Wrapper class.

    Now my best explaination of the bug
    "
    It works great up to a list of 3, or if the list can fit all the files.
    Other than that, I was trying it with a directory with 16 files, the first 3 were correct, but the 4th on the list was actually the 7th biggest in the directory -- [using a list of 4].
    I can't figure out the bug for the life of me.
    "
    [sent to my teacher along with my code, YES IT IS A COLLEGE PROJECT, but I ALREADY HANDED IT IN... even though I handed it in already -- I wanna figure out whats wrong with it]


  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Finding biggest files, making list

    If you want the entire source for compilation purposes, let me know and I'll post it.

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Finding biggest files, making list

    Well I figured out what the problem was, my adding after filling up the array was flawed
    		for(int x = 0; x<CompFiles.length; x++)
    		{
    			int stat = c.compareTo((Wrapper) CompFiles[x]);
    			if(stat > 0)
    			{
    				CompFiles[x]=c;
    				return;
    			}
    		}
    Bascially say I have the 2nd largest, 3rd largest, and 4th largest filled up the array of 4. But I have 5 files. I find the 5th file, check the [0] of the array first, which would be the 2nd largest, and figure OH LOOK ITS LARGER and throw that out.
    So I had to find the smallest in that array, adn then check to see if its bigger than the file I'm looking to add.
    	public void add(Comparable<Wrapper> c)
    	{
    		for(int x =0; x<CompFiles.length ; x++)
    		{
    			if(CompFiles[x] == null )
    			{
    				CompFiles[x] = c;
    				return;
    			}
    		}
    		int smallestFileIndex=0;
    		long smallestFileSize=((Wrapper)CompFiles[0]).getSize();
    		for(int x=0; x<CompFiles.length; x++)
    		{
    			if(((Wrapper)CompFiles[x]).getSize()<smallestFileSize)
    				smallestFileIndex=x;
    		}
    		if(((Wrapper)CompFiles[smallestFileIndex]).getSize()<((Wrapper) c).getSize())
    			CompFiles[smallestFileIndex]=c;
    	}
    As you see, I just had to add a check thats it.

Similar Threads

  1. Problem Finding Value from List
    By WhiteSoup12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 11th, 2012, 06:24 PM
  2. Making Executable Jar Files.
    By Java Programmer in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: January 19th, 2012, 12:14 AM
  3. Help, Building List & finding min and max
    By Margaret_Girl87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 30th, 2011, 07:15 PM
  4. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  5. Finding min in linked list
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 02:06 AM