# sorting

• 09-30-2008, 01:59 PM
jot321
sorting
/*INPUT TWO STRING ARRAYS AND SORT THEM*/
class manyarrays
{
public void main(String arr1[],String arr2[])
{
int len=arr1.length;
int len2=arr2.length;

String arr3[]=new String [l];
String arr4[]=new String [l1];

manyarrays obj=new manyarrays();

c[]=obj.sort(ab[],l);//error(not a statement)
d=obj.sort(b[],l1);

for(int z=0;z<l;z++)
System.out.println(arr3[z]);

for(z=0;z<l1;z++)
System.out.println(arr4[z]);

}

String sort(String a[] ,int l)
{
int z,j;
String temp;
for(z=0;z<l;z++)
{
for(j=0;j<l-1;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

return a;
}
}

/*
variable z--loop
j--loop
len--length of first inputed array
len2-length of second inputed array

*/
• 09-30-2008, 02:11 PM
Eranga
You don't have define c[] in your code.

Instantiation of arr3 and arr4 is wrong, length is not define.

Variable b is not define in your code.

Two for loops are incorrect.
• 09-30-2008, 02:23 PM
jot321
/*INPUT TWO STRING ARRAYS AND SORT THEM*/
class manyarrays
{
public void main(String arr1[],String arr2[])
{
int len=arr1.length;
int len2=arr2.length;

String arr3[]=new String [len];
String arr4[]=new String [len2];

manyarrays obj=new manyarrays();

arr3[]=obj.sort(arr1[],l);//error(not a statement)
arr4[]=obj.sort(arr2[],l1);

for(int z=0;z<l;z++)
System.out.println(arr3[z]);

for(z=0;z<l1;z++)
System.out.println(arr4[z]);

}

String sort(String a[] ,int l)
{
int z,j;
String temp;
for(z=0;z<l;z++)
{
for(j=0;j<l-1;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

return a;
}
}

/*
variable z--loop
j--loop
len--length of first inputed array
len2-length of second inputed array

*/

stilll error is there
• 09-30-2008, 02:46 PM
Eranga
Yes it is. Still you don't have correct some points. In that statement l is not define.
• 09-30-2008, 02:50 PM
Norm
Eranga,
How do you see the errors? I don't see where the OP has posted them.

Ok I just saw them. They were hidden in the code. I guess I need better glasses to see them. A bigger banner like:<<<<<<<<<<<<<<<<<< HERE <<<<<<<<<<<<<<<<<
would make it easier.
• 09-30-2008, 03:13 PM
jot321
solution to my error
• 09-30-2008, 03:14 PM
jot321
/*INPUT TWO STRING ARRAYS AND SORT THEM*/
class manyarrays
{
public void main(String arr1[],String arr2[])
{
int len=arr1.length;
int len2=arr2.length;

String arr3[]=new String [len];
String arr4[]=new String [len2];

manyarrays obj=new manyarrays();

arr3[]=obj.sort(arr1[],len);//error(not a statement)
arr4[]=obj.sort(arr2[],len2);

for(int z=0;z<l;z++)
System.out.println(arr3[z]);

for(z=0;z<l1;z++)
System.out.println(arr4[z]);

}

String sort(String a[] ,int l)
{
int z,j;
String temp;
for(z=0;z<l;z++)
{
for(j=0;j<l-1;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

return a;
}
}

/*
variable z--loop
j--loop
len--length of first inputed array
len2-length of second inputed array

*/

<<<<<<<<<<<<<<<<stilll same error is there>>>>>>>>>>>>>
• 09-30-2008, 03:30 PM
Eranga
Quote:

Originally Posted by Norm
Eranga,
How do you see the errors? I don't see where the OP has posted them.

Ah, that's way I learn Java pal. ;) First read the line by line in my mind, my lecturer call is dry-run. Easy to identify any error, if you the basis well. :)
• 09-30-2008, 03:34 PM
Eranga
Quote:

Originally Posted by jot321
<<<<<<<<<<<<<<<<stilll same error is there>>>>>>>>>>>>>

Yes it is, your sort() function return a string and you try to assign it into an array. It's not possible.
• 09-30-2008, 05:13 PM
jot321
yes you are right but it cannot return a array also
so what can i do
• 10-01-2008, 03:46 AM
Eranga
Why you can't return an array from that method? Did you try, and get any errors? If so, what the error message says?
• 10-01-2008, 06:35 AM
fishtoprecords
This smells like homework to me. Part of learning is learning how to debug
• 10-01-2008, 07:31 AM
Eranga
And learning fundamentals. Seems he doesn't know about returning values from a method.
• 10-01-2008, 09:11 AM
jot321
/*INPUT TWO STRING ARRAYS AND SORT THEM*/
class manyarrays
{
public void main(String arr1[],String arr2[])
{
int len=arr1.length;
int len2=arr2.length;

String arr3[]=new String [len];
String arr4[]=new String [len2];

manyarrays obj=new manyarrays();

arr3=obj.sort(arr1,len);
arr4=obj.sort(arr2,len2);

for(int z=0;z<l;z++)
System.out.println(arr3[z]);

for(z=0;z<l1;z++)
System.out.println(arr4[z]);

}

String sort(String a[] ,int l)
{
int z,j;
String temp;
for(z=0;z<l;z++)
{
for(j=0;j<l-1;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

return;
}
}

returning array
error incompatible data types
found java.lang.String but expected java.lang.string[]
• 10-01-2008, 10:49 AM
Eranga
You have nothing is returning here.

Code:

`return;`
• 10-01-2008, 11:04 AM
jot321
then what is the solution
• 10-01-2008, 11:45 AM
Eranga
If you want to return a string you should return that, if you want to return an array, you should do that.

Do you know how to write a method in Java and return a value? Seems to me you don't.
• 10-02-2008, 10:10 AM
jot321
how can we return an array
• 10-02-2008, 10:30 AM
Eranga
You should learn how to methods are define in Java.

Code:

```    public static int[] method_name() {          int[] a = new int[5];         return a;     }```