Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By gimbal2

Thread: incompatible type

  1. #1
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Red face incompatible type

    i am new in java..
    i having a problem when doing merge sort.
    this is my coding...

    Java Code:
    import java.util.*;
    
    public class Person
    {
        public static void main(String[] args) 
        {
            String[] list = {"\n First name: Firdaus, Last name: Ahmad, Age: 24", 
                "\n First name: Hanani, Last name: Saad, Age: 59", 
                "\n First name: Zarina, Last name: Zakaria, Age: 37", 
                "\n First name: Celina, Last name: Paul, Age: 37",  
                "\n First name: Ridzuan, Last name: Rahman, Age: 43", 
                "\n First name: Mazlan, Last name: Samad, Age: 21", 
                "\n First name: Aina, Last name: Daud, Age: 29", 
                "\n First name: Syafiq, Last name: Muhamad, Age: 72", 
                "\n First name: Iman, Last name: Rosli, Age: 22", 
                "\n First name: Pavithra, Last name: Selvam, Age: 18"};
            
            System.out.println("Before sorting:" +Arrays.toString(list));
            mergeSort(list); // merge sort the elements
            System.out.println("After sorting:" +Arrays.toString(list));
        }
    
        // Places the elements of the given array into sorted order
        // using the merge sort algorithm.
        // post: array is in sorted (nondecreasing) order
        public static String[] mergeSort(String[] list) 
        {
            String [] sorted = new String[list.length];
            if (list.length == 1) 
            {
                sorted = list;
            } 
            else 
            {
                
                int mid = list.length/2;
                
                String[] left = null; 
                String[] right = null;
                    
                if ((list.length % 2) == 0)
                {
                    left = new String[list.length/2];
                    right = new String[list.length/2];
                }
                else
                { 
                    left = new String[list.length/2];
                    right = new String[(list.length/2)+1];
                }
                
                int x=0;
                int y=0;
                for (x=0; x < mid; x++) 
                {
                    left[x] = list[x];
                }
                
                for (x=0; x < list.length; x++) 
                {
                    right[y++] = list[x];
                }
                left = mergeSort(left);
                right = mergeSort(right);
                sorted = mergeArray(left,right);
            }
            return sorted;
        }
        
        // Merges the given nameA and nameB arrays into the given result array. 
        // pre : result is empty; left and right are sorted
        // post: result contains result of merging sorted lists;
        public static void mergeArray(String[] left, String[] right)
        {
            while (lIndex < left.length || rIndex < right.length) 
            {
                if (lIndex == left.length) 
                {
                    merged[mIndex++] = right[rIndex++];
                }
                else if (rIndex == right.length) 
                {
                    merged[mIndex++] = left[lIndex++];
                } 
                else 
                { 
                    comp = left[lIndex].compareTo(right[rIndex]);
                    if (comp > 0) 
                    {
                        merged[mIndex++] = right[rIndex++];
                    } 
                    else if (comp < 0) 
                    {
                        merged[mIndex++] = left[lIndex++];
                    } 
                    else 
                    { 
                        merged[mIndex++] = left[lIndex++];
                    }
                } 
            }
            return merged;
        }
    }

    the error is incompatible type at 'sorted = merge(left,right);'
    please help me.. it almost 3 weeks i'm trying to fix it.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: incompatible type

    Where is the variable: sorted defined?

    What does the merge() method return that can be assigned to a variable?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: incompatible type

    What types of variables does that method take?

    What types of variables are you giving it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    I am sorry. I really do not know. Actually this coding corrected by my tutor when I asked about this offense he asked me to find the answers myself. I have tried many times, but do not know.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: incompatible type

    I agree with your tutor. You need to really understand what this code is doing instead of just trying random things.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    i'm sorry...
    almost 3 weeks i'm trying...
    it still a lot of mistakes when i fix it...
    this merge sort need to be submit tomorrow...

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: incompatible type

    If you can't answer the 2 questions I asked, let me make it easier by only asking one question. Please try to answer it:

    What does the mergeSort() method return that can be assigned to a variable?



    ???? Is the source in post#1 being changed? Every time I look at it it's different!!!
    For example this is no longer in the source:
    the error is incompatible type at 'sorted = merge(left,right);'
    Last edited by Norm; 04-10-2014 at 07:50 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    i'm sory...
    i changed the code because i mistakenly copy the coding.
    the error is incompatible type at 'sorted = merge(left,right);'
    is suppose to be this one ' sorted = mergeArray(left,right);'

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: incompatible type

    Ok, try this one question:
    What is the mergeArray() method defined to return that can be assigned to a variable?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    yes i think... it is to return the value of array that been merged after sorting

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: incompatible type

    When a method is defined, how is written to say that it will return the value of array?

    See the tutorial on how to define a method: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    look especially about the return type.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    thank you...now I understand...
    I've change the code form 'public static void mergeArray(String[] left, String[] right)'
    to 'public static String[] mergeArray(String[] left, String[] right)'.
    the error solved buttt...
    there new error appear.
    it says cannot find symbol - variable lIndex under green code

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: incompatible type

    cannot find symbol - variable lIndex
    Where is that variable defined? The compiler can not find a definition for it.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    it mean i need to declare the lIndex right?
    how about 'rIndex', 'mIndex', 'merged', 'comp'?
    if i need to declare all of this, it suppose to be int or String?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: incompatible type

    suppose to be int or String?
    If you can't figure out the data type for the variable, you are going to have a very hard time fixing this program.

    Why are you just starting programming with this hard program? You need to start with many much simpler programs to build the knowledge and experience that is needed to fix this program.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    atiqahazmi is offline Member
    Join Date
    Apr 2014
    Posts
    8
    Rep Power
    0

    Default Re: incompatible type

    yes I know ... But my tutor assign this topic to me. But my other friend was given the bubble sort, selection sort, quick sort, etc. In my class there are 7 people and has 7 different questions. Unfortunately, I was able to this question.

    it ok..i'll try my best..
    i'll ask again if i having a problem.

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,877
    Rep Power
    5

    Default Re: incompatible type

    I wonder: you cannot ask your classmates for help? Doing these things alone is really unproductive, and as you see a forum is not a substitute for a study group or a tutor.

    Its a futile question I know because every time I ask it I get the most horrifying answers of people working under the most terrible of circumstances under the suppressing thumb of evil teachers and lazy classmates, but I just have to remember the happy days where I was learning programming with my classmates and I just wish people today could share that joy I felt back then... I wouldn't have made it if it wasn't for the support I got from my class.
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Incompatible type
    By SuitedSlime in forum New To Java
    Replies: 2
    Last Post: 09-15-2013, 08:02 PM
  2. Replies: 4
    Last Post: 08-01-2011, 10:29 AM
  3. Why am I getting the error: Incompatible Type
    By bigsonny in forum New To Java
    Replies: 4
    Last Post: 07-31-2011, 02:41 AM
  4. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  5. Help in incompatible type JTextArea
    By sj_420 in forum Advanced Java
    Replies: 4
    Last Post: 04-15-2010, 03:00 AM

Tags for this Thread

Posting Permissions

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