0

I want to make a Bubble Sort Program in Java, but I'm feel trouble. I also being confused with static void and also public static void. Actually, I want to convert this program from C++ to Java, but the program have an errors.

This is the Java program that I have created :

import java.util.Scanner;

public class BubbleSort{
    static Scanner scan = new Scanner(System.in);
    int data[] = new int[20];
    static int Nilai[] = new int[20];

    static void prosesPerpindahan(int data[],int y){
        int x;
        for(x=0;x<y;x++){
            System.out.print(data[x]+" ");
        }
    }

    static void prosesSorting(int data[],int y){
        int proses,x,tampung;
    
        for(proses=1;proses<y;proses++){
            for(x=0;x<y-proses;x++) {
                if(data[x]>data[x+1]){
                    tampung=data[x];
                    data[x]=data[x+1];
                    data[x+1]=tampung;
                }
            }
        System.out.print("Tahap Ke-"+proses+" : ");
        prosesPerpindahan(data,y);
        }
    }

    public static void main(String args[]){
        Sort tst = new Sort();
        int i, N;
        
        System.out.print("Masukkan Banyak Bilangan : ");
        N = tst.nextInt();
        
        for(i=0; i<N; i++){
            System.out.print("Elemen ke-"+i+" : ");
            Nilai[i] = tst.nextInt();
        }
        
        prosesSorting(Nilai,N);

        System.out.println("Setelah Di Urutkan dengan Bubble Sort : ");
        prosesPerpindahan(Nilai,N);
    }
}

When I want to compile that program, will appear this notes :

BubbleSort.java:32: error: cannot find symbol
        Sort tst = new Sort();
        ^
  symbol:   class Sort
  location: class BubbleSort
BubbleSort.java:32: error: cannot find symbol
        Sort tst = new Sort();
                       ^
  symbol:   class Sort
  location: class BubbleSort
2 errors

Note : Please answer this question if you can!

  • 1
    It doesn't look like you've imported class `Sort` so how should Java know where to find it? What is `Sort` meant to be anyway? You're using it like a `Scanner` (`tst.nextInt();`) so did you actually mean to use that class or the variable you've already declared, i.e. `scan `? – Thomas Jan 05 '22 at 12:22
  • 1
    your class is named BubbleSort, not Sort – Stultuske Jan 05 '22 at 12:23
  • 1
    The fact that you are trying to call `tst.nextInt();` on your `Sort tst = new Sort();` object heavily implies that `tst` is supposed to be a `java.util.Scanner` object. – OH GOD SPIDERS Jan 05 '22 at 12:26
  • @Stultuske that was my thought as well but `Sort` seems not to refer to `BubbleSort` in any case, i.e. `BubbleSort` doesn't have any instance methods (only a field `data`) and the OP is calling `tst.nextInt()` which indicates this doesn't refer to anything related to a bubble sort. – Thomas Jan 05 '22 at 12:27

0 Answers0