Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anybody please help me debugging this code? I don't know why but it says compilation error.

Solution.java:7: error: '{' expected
public class bubbleSort(int[] a) {
                       ^
Solution.java:7: error: ';' expected
public class bubbleSort(int[] a) {
                               ^
Solution.java:9: error: illegal start of expression
        public bubbleSort(int[] a){
        ^
Solution.java:9: error: '.class' expected
        public bubbleSort(int[] a){
                                ^
Solution.java:9: error: ';' expected
        public bubbleSort(int[] a){
                                 ^
Solution.java:56: error: reached end of file while parsing
}
 ^
6 errors


What I have tried:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class bubbleSort(int[] a) {
        
        public bubbleSort(int[] a){
        int endPosition = a.length - 1;
        int swapPosition;

        while( endPosition > 0 ) {
            swapPosition = 0;
            swapNum =0;

            for(int i = 0; i < endPosition; i++) {

                if( a[i] > a[i + 1] ){
                    // Swap elements 'i' and 'i + 1':
                    int tmp = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = tmp;

                    swapPosition = i;
                    swapNum++;
                } 

                
            } 

            
        } 

        
        firstElement = a[0];
        lastElement = a[a.length -1];
        System.out.println("Array is sorted in "+ swapNum + " swaps.");
        System.out.println("First Element: "+ firstElement);
        System.out.println("Last Element: "+ lastElement);
    }
 } // end bubbleSort

public class Solution {
     
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for(int a_i=0; a_i < n; a_i++){
            a[a_i] = in.nextInt();
        }
        
        bubbleSort instance = new bubbleSort(a);
        instance(a);
    }
}
Posted
Updated 16-May-20 5:08am
v2
Comments
MadMyche 15-May-20 15:05pm    
Usually it gives a reason for the compilation error.
Member 14815736 15-May-20 15:16pm    
it has, but I am unable to fix it.
Patrice T 15-May-20 15:14pm    
Compilation error also tells reason and line number.
Member 14815736 15-May-20 15:16pm    
yes! it has in fact 6, all in one line. I know all of them are related but I am not able to fix it!
Patrice T 15-May-20 15:34pm    
And you plan to tell us ?

1 solution

Are you sure a class is declared with parameters ?
Java
public class bubbleSort(int[] a) {
 
Share this answer
 
Comments
MadMyche 15-May-20 16:58pm    
+5 (ding ding ding... we got a winner)
Patrice T 15-May-20 17:28pm    
Thank you. :)
[no name] 16-May-20 11:17am    
+5
Patrice T 16-May-20 12:35pm    
thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900