Error in my code

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int[] arr=new int[5];
for(int i=0;i<arr.length;i++)
{
arr[i]=scn.nextInt();
}
bubblesort(arr);
display(arr);

}
public static void bubblesort(int[] arr){
    for(int counter=0;counter<arr.length-1;counter++)
    {
        for(int j=0;j<(arr.length-1)-counter;j++)
        {
            if(arr[j]>arr[j+1])
            {int temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
        }
    }
}
} 
public static void display(int[] arr){
    for(int i=0;i<arr.length;i++)
    {
        System.out.println(arr[i]);
    }
}

}import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int[] arr=new int[5];
for(int i=0;i<arr.length;i++)
{
arr[i]=scn.nextInt();
}
bubblesort(arr);
display(arr);

}
public static void bubblesort(int[] arr){
    for(int counter=0;counter<arr.length-1;counter++)
    {
        for(int j=0;j<(arr.length-1)-counter;j++)
        {
            if(arr[j]>arr[j+1])
            {int temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
        }
    }
}
} 
public static void display(int[] arr){
    for(int i=0;i<arr.length;i++)
    {
        System.out.println(arr[i]);
    }
}

}
pls look what wrong in code

i also corrected my code with Scanner scn=new…line

still giving error wshat to do

Hi Dhruv,
You had hardcoded your code for the value 5 as seen in the sample input.Instead we need to make our code generic for the n-sized Array always.
Otherwise your logic was completely correct.


I have modified it for you.
Thankyou,and please mark the doubt as resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.