Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Norm

Thread: Error when doing bubble sort using a text file

  1. #1
    welshcrossy is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Error when doing bubble sort using a text file

    Hello can anyone please advise where i am going when with this please?

    I am getting the below message:

    run:
    Values Before the sort
    14 James Anne Bill Maria Bob Jill Elvis Carol Dennis Mandy Steven Sian Harry Linda
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at bubblesort2.Bubblesort2.main(Bubblesort2.java:39)


    The contents of the text file is:

    14
    James
    Anne
    Bill
    Maria
    Bob
    Jill
    Elvis
    Carol
    Dennis
    Mandy
    Steven
    Sian
    Harry
    Linda

    Below is my code:


    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    Java Code:
    package bubblesort2;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Bubblesort2{  
        
            public static void main(String a[])throws Exception{  
        
            String inputFile = "names.txt";    
                
            List<String> lineList = new ArrayList<String>();  
            FileReader fileReader = new FileReader(inputFile);
    	BufferedReader bufferedReader = new BufferedReader(fileReader);       
            //String st="";  
            String inputLine;
            while ((inputLine = bufferedReader.readLine()) != null) {
    		lineList.add(inputLine);
    	} 
            int i;
            String Array[] = toStrArray(lineList);
            System.out.println("Values Before the sort");
            for(i = 0; i < Array.length; i++)  
            System.out.print(Array[i]+"  ");
            System.out.println();
            
            
            bubble_srt(array, Array.length);
            System.out.print("Values after the sort");
            for(i = 0; i <Array.length; i++){
            System.out.print(Array[i]+"  ");
            }
            
            System.out.println();
            }  
        
            public static void bubble_srt(int a[], int n){
                int i, j,t=0;
                for(i = 0; i < n; i++){
                    for(j = 1; j < (n-i); j++){
                        if(a[j-1] > a[j]){
                            t = a[j-1];
                            a[j-1]=a[j];
                            a[j]=t;
                        }
                    }
                }
            }
            static String[] toStrArray(List<String> stringlineList) {
                String[] strArray = new String[stringlineList.size()];
                for (int i = 0; i < stringlineList.size(); i++) {
                    strArray[i] = stringlineList.get(i);
                }            return strArray;
            }
    }
    Last edited by Norm; 04-14-2012 at 07:15 PM. Reason: added code tags

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

    Default Re: Error when doing bubble sort using a text file

    Can you compile the program with javac to get the full text of the error message?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    welshcrossy is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Error when doing bubble sort using a text file

    Thanks for the quick reply norm. How do I do this in Netbeans?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Error when doing bubble sort using a text file

    Sorry, I have no idea how to use your IDE.

    Try using the javac command from a command prompt
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Error when doing bubble sort using a text file

    Where is array declared:

    Java Code:
    bubble_srt(array, Array.length);

  6. #6
    welshcrossy is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Error when doing bubble sort using a text file

    Thank You shall,

    Can you explain please why this is happening? I've spent hours looking over this and by now my mind is mush.

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

    Default Re: Error when doing bubble sort using a text file

    Why doesn't the IDE tell the OP about that?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Error when doing bubble sort using a text file

    The error is probably you declared "String[] Array" and use "array" ... I advise NOT to use capital letters for variable names... for your code the line has to be like:
    bubble_srt(Array, Array.length);

  9. #9
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Error when doing bubble sort using a text file

    make the following correction:

    Java Code:
            bubble_srt(Array, Array.length);
    and

    Java Code:
        public static void bubble_srt(String a[], int n)
        {
            int i, j;
            String temp;
            for (i = 0; i < n; i++)
            {
                for (j = 1; j < (n - i); j++)
                {
                    if (a[j-1].compareTo(a[j]) > 0)
                    {
                        temp = a[j - 1];
                        a[j - 1] = a[j];
                        a[j] = temp;
                    }
                }
            }
        }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Error when doing bubble sort using a text file

    We're not supposed to be doing the coding for the OPs. They are not going to learn from copy and pasting solutions that you provide.
    Read this:
    The Problem with Spoon-feeding
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Error when doing bubble sort using a text file

    In Netbeans, you should see a little light bulb next to the line that has the problem. Right click on it and for this situation it would have told you to add a integer array member variable named array. However, the problem is that you really wanted to bubble sort string (i.e. names of people).

  12. #12
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Error when doing bubble sort using a text file

    Quote Originally Posted by Norm View Post
    We're not supposed to be doing the coding for the OPs. They are not going to learn from copy and pasting solutions that you provide.
    Read this:
    The Problem with Spoon-feeding
    I'm new to Java myself and "most" of the time I'm learning as I solving another person's problem.

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

    Default Re: Error when doing bubble sort using a text file

    Its good you want to help. Try to think of ways to help the OP solve the problem without actually posting the code.
    When you have solved the problem, try to tell the OP the steps you took to do it.
    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 18
    Last Post: 05-02-2011, 05:37 PM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:42 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

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
  •