Results 1 to 19 of 19

Thread: sorting

  1. #1
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default 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


    */

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    /*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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default

    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.

  6. #6
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    solution to my error

  7. #7
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    /*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>>>>>>>>>>>>>

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    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. :)

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jot321 View Post
    <<<<<<<<<<<<<<<<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.

  10. #10
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    yes you are right but it cannot return a array also
    so what can i do

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

    Default

    Why you can't return an array from that method? Did you try, and get any errors? If so, what the error message says?

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    13

    Default

    This smells like homework to me. Part of learning is learning how to debug

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

  14. #14
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    /*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[]

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

  16. #16
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    then what is the solution

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  18. #18
    jot321 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    how can we return an array

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    26

    Default

    You should learn how to methods are define in Java.

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

Similar Threads

  1. CompilationUnit sorting
    By rhub in forum Eclipse
    Replies: 0
    Last Post: 09-18-2008, 01:22 PM
  2. Sorting CachedRowset
    By Sayed in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2008, 12:14 PM
  3. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  4. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:13 AM
  5. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 06:29 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
  •