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?
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
}
}
Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo
Take a look at the brackets! (in line 8!)
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.
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?
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.
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.
Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo
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...:)
Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo
Quote:
Originally Posted by
Tolls
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.
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 :(.
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?
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
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.
Re: Major Problems With Creating A Program To Read An Array and Sort Using CompareTo
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.
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.