Results 1 to 5 of 5
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Question Whats wrong with this code???

    Java Code:
    public class Try {
    	public static void main(String[] args)  {
    		//create an integer array
    		
    		Integer[] intArray = {new Integer(2), new Integer (4),new Integer(4), new Integer (3)};
    		
    		
    		sort(intArray);
    		printList(intArray);
    }
    	
    	
    	public static void sort(Comparable list[]){
    		
    		
    		Comparable currentMax;
    		int currentMaxIndex;
    		
    
    		for(int i = 0; i < list.length -1; i++){
    			for (int j = 0; j <list.length; j++){
    				
    				if(list[i].compareTo(list[j]) < 0){
    					
    					Comparable temp = list[i];
    					list[i] = list[j];
    					list[j] = temp;
    					
    				}
    			}
    		}	
    	}
    	
    	public static void printList(Object[] list){
    		for(int i = 0; i <list.length; i++){
    			System.out.print(list[i] +" ");
    			System.out.println();
    		}
    	}
    	}
    Whats wrong ,, please help ??:confused::confused:
    Moderator Edit: Code tags corrected
    Last edited by Fubarable; 03-28-2010 at 02:24 PM. Reason: [color="blue"]Moderator Edit: Code tags corrected[/color]

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    Whats wrong ,, please help ??:confused::confused:
    What do you mean by 'wrong'? Compilation errors? Runtime errors? Is an exception thrown or doesn't the code do what you want? Please elaborate.

    kind regards,

    Jos

  3. #3
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default

    It doesnt sort them right !!
    It works but sorts wrong !!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    It doesnt sort them right !!
    It works but sorts wrong !!
    That's just because your sort method is wrong. Put in some System.out.println statements and see for yourself which elements (i and j) are compared and swapped.

    kind regards,

    Jos

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    It looks like you copy-and-pasted your sort() method from somewhere, and that you don't understand it. How do I know that?

    1. Your sort() method wants a list of a type that's not really a type at all, but an Interface.
    2. Your sort() method would be much more convenient if it called for a list of the same type that your list really is.
    3. Your sort() method declares two local variables right away, and then never uses them.

    You should start from the beginning again. Take out a deck of cards, and pull out just the ace through 10 of all four suits. Shuffle them, and deal out a row of ten cards on a table, face up. Sort them according to these rules:
    1. You may pick up only one card at a time, and only with your right hand.
    2. You may only lay a card down into one of the original ten spaces.
    3. You may temporarily put one card in your left hand and hold it there while you pick up and lay down another card with your right hand.
    Think about what you are doing while you sort them. Write down each step. Shuffle all forty cards again and deal out ten more. Sort them again, carefully following the instructions you wrote down last time (this is your algorithm). Did it work? Keep doing this with real, physical cards until you have an algorithm that you understand thoroughly and that works correctly.

    Now you are ready to write your code. Your array is the ten spaces on the table. Your right hand is the index of your loop. Your left hand is a temporary variable.

    Show us what you come up with.

    -Gary-

Similar Threads

  1. Database help... whats wrong?
    By neosnokia in forum JDBC
    Replies: 4
    Last Post: 06-09-2009, 11:17 PM
  2. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 03:51 AM
  3. Whats wrong with my maths???
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 11-03-2008, 05:52 PM
  4. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:57 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •