Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    I know what I've written is probably unadulterated rubbish but is there any chance its close to what I want.

    I need it to read in args into an array - given earlier checks it appears to do that. The problems start when I need it to sort the strings, i.e. user enters Joe Alan Barry David Fred, the program should sort these into Alan Barry David Fred Joe

    but what I have written when I go to compiler spits out no less than 38 errors which must mean what I've written is utter cr*p. Is it repairable and if so where should I be looking?

    Java Code:
    public class names {
    public static void main(String[] args) {
    for(int i=0; i< args.length;i++){
    
    System.out.print(i+"=");
    System.out.print(args[i] +" ");
    }
    }
    for(int i=args.length-1; i>0; i--)
    {
    int maxLocation = 0;
    for(int j=1;j <=i; j++){
    if ( args[j].compareTo(args[maxLocation]) >0){
    maxLocation=j;
    }
    }
    // swap max with list[i] if necessary
    }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    Take a look at the brackets! (in line 8!)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    You've used code tags, which is great.
    Sadly your code has next to no formatting.
    Format your code properly and you'll probably see what the compiler is complaining about...38 errors in a 19 line bit of code implies brackets are out of synch or something.

  4. #4
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    Thanks everybody. That issue is sorted. Now at least the program compiles. However, it compiles but doesn't work 100%.

    say the user enters java names William Billy Edward

    then the program should print

    0. William 1. Billy 2. Edward 0. Billy 1. Edward 2. William

    The first lot in the order entered, the second in the alphabetical.

    However, it doesn't do this.

    I've followed my lecturers instructions as clearly as I can and I've used CompareTo but the program doesn't seem to be storing the alphabetical list anywhere.

    I've gotten this far (below). One idea I have is perhaps I need to save two arrays?

    Java Code:
    public class names {
    public static void main(String[] args) {
    
    	for(int i=0; i< args.length;i++)
    	{
    	System.out.print(i+"=");
    	System.out.print(args[i] +" ");
    	}
    
    	for(int i=args.length-1; i>0; i--)
    	{
    	int maxLocation = 0;
    	for(int j=1;j <=i; j++)
    		{
    	if ( args[j].compareTo(args[maxLocation]) >0)
    			{
    	maxLocation=j;
    			}
    		}
    	}
    
    	for (int j=0; j< args.length;j++)
    	{
    
    	System.out.print(j+"=");
    	System.out.print(args[j] +" ");
    	}
    
    	}
    }
    I'm not the best for indentation, don't really understand it too well, but I hope the user who recommended I try doing it doesn't feel I've ignored his advice as I have had a go at it.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    You really do need to sort out your formatting.
    Use spaces rather than tabs as tab settigs are different in different environments...for example the above is a bit of a mess.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    Java Code:
    public class names {
        public static void main(String[] args) {
            for(int i=0; i< args.length;i++)
            {
                System.out.print(i+"=");
                System.out.print(args[i] +" ");
            }
    
            for(int i=args.length-1; i>0; i--)
            {
                int maxLocation = 0;
                for(int j=1;j <=i; j++)
                {
                    if ( args[j].compareTo(args[maxLocation]) >0)
                    {
                       maxLocation=j;
                    }
                }
            }
    
            for (int j=0; j< args.length;j++)
            {
                System.out.print(j+"=");
                System.out.print(args[j] +" ");
            }
        }
    }
    That's better...:)

  7. #7
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    Quote Originally Posted by Tolls View Post
    You really do need to sort out your formatting.
    Use spaces rather than tabs as tab settigs are different in different environments...for example the above is a bit of a mess.
    Ok, here is an attempt, its probably still a mess though.

    Java Code:
    public class names {
    public static void main(String[] args) {
    
     for(int i=0; i< args.length;i++)
     {
     System.out.print(i+"=");
     System.out.print(args[i] +" ");
     }
    
     for(int i=args.length-1; i>0; i--)
     {
      int maxLocation = 0;
      for(int j=1;j <=i; j++)
      {
       if ( args[j].compareTo(args[maxLocation]) >0)
       {
       maxLocation=j;
       String temp=args[j];
       args[j]=args[i];
       args[i]=temp;
       }
      }
     }
    
     for (int j=0; j< args.length;j++)
     {
      System.out.print(j+"=");
      System.out.print(args[j] +" ");
     }
    
     }
    }
    I've gotten it this far, sometimes it works and sometimes it doesn't. How can I finally nail it? Spent about 5 hours at it so far which seems a bit ridiculous for one program :(.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    So what does that code produce for the times it fails, compared to what it should produce?

  9. #9
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    It should produce for example

    (user input) java names Dario Billy Andrew Joshua

    0. Dario 1. Billy 2. Alan 3. James 0. Alan 1. Billy 2. Dario 3. James

    but it producese


    (user input) java names Dario Billy Andrew Joshua

    0. Dario 1. Billy 2. Alan 3. James 0. Dario 1. Billy 2. Alan 3. James

  10. #10
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    Look at your if statement that compares two Strings and swaps them if they're out of order. You compare args[j] with args[maxLocation], but then you swap args[j] with args[i]. It's inconsistent.

  11. #11
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    Java Code:
    public class names {
    public static void main(String[] args) {
     
     for(int i=0; i< args.length;i++)
     {
     System.out.print(i+"=");
     System.out.print(args[i] +" ");
     }
     
     for(int i=args.length-1; i>0; i--)
     {
      int maxLocation = 0;
      for(int j=1;j <=i; j++)
      {
       if ( args[j].compareTo(args[maxLocation]) >0)
       {
       maxLocation=i;
       String temp=args[j];
       args[j]=args[i];
       args[i]=temp;
       }
      }
     }
     
     for (int j=0; j< args.length;j++)
     {
      System.out.print(j+"=");
      System.out.print(args[j] +" ");
     }
     
     }
    }
    I changed it by changing the max location to i but it still doesn't work properly. I am even more confused now.

  12. #12
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo

    But you changed maxLocation to i AFTER you did the comparison, so the swap and the comparison are still inconsistent with each other. If you set maxLocation equal to i BEFORE the comparison, it would be closer to correct, but still not quite so, because your j loop starts at 1 instead of 0, while array indices start at 0.

Similar Threads

  1. Problem implementing compareTo for Arrays.sort
    By FallenBlade in forum New To Java
    Replies: 10
    Last Post: 11-07-2011, 07:08 PM
  2. compareTo() problems...
    By falkon114 in forum New To Java
    Replies: 1
    Last Post: 01-28-2011, 09:08 PM
  3. Sort with CompareTo
    By ScienceLife in forum New To Java
    Replies: 7
    Last Post: 12-01-2010, 01:08 PM
  4. cant get compareTo to read false
    By erin.ctm in forum New To Java
    Replies: 6
    Last Post: 11-05-2010, 06:33 PM
  5. Problems with sort swimmers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 06:43 AM

Posting Permissions

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