1

I wrote a simple code for bubble sort and used a swap function which accepts reference of objects as its parameter. I am getting the following error:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Main.main(Main.java:6)

My code:

import java.util.Scanner;   
public class Bubble {   
    public static void main(String[] args){   
        Scanner scan = new Scanner(System.in);    
        int n= scan.nextInt();   
        int[] arr = new int[n];   
        for(int i = 0; i<n ; i++){  
            arr[i] = scan.nextInt();   
        }  
        bubbleSort(arr);  
        for(int i = 0; i<n ; i++){  
            System.out.print(arr[i]+"");  
        }   
    }  
    static int[] bubbleSort(int[] arr){

        int swapped = 1;
        while(swapped!= 0){
            swapped = 0;
            for(int i=1; i< arr.length; i++){
                if(arr[i-1]>arr[i]){
                    Example a = new Example(arr[i-1]);
                    Example b = new Example(arr[i]);
                    swap(a,b);
                    swapped++;
                }
            }
        }
        return arr;
    }

    static void swap(Example a, Example b){
        Example temp = new Example(0) ;
        temp.a = a.a;
        a.a = b.a;
        b.a= temp.a;
    }
}
class Example{
    int a ;
    public Example(int a){
        this.a = a;
    }
}

How can I catch these errors and why am I getting these?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Anonymous
  • 13
  • 3
  • 1
    Can't seem to get the same error by running your code, what input did you use? – RRIL97 Aug 30 '20 at 06:56
  • What you're trying to do with `Example` will not work. `Example` only contains the _value_ of the int you put into it. Altering `example.a` will not alter your array. – khelwood Aug 30 '20 at 06:58
  • Have to agree with @LL_MM. [Cannot reproduce the problem](https://ideone.com/riksRx). – Turing85 Aug 30 '20 at 06:59
  • when I executed this on my IDE it kind off went into some infinite loop because it wasn't stopping ..for example I gave the following input : 5 1,2,4,5,3...but even after this it didn't execute and we could still give more inputs to it...(which shouldn't happen)... So I ran this on an Online IDE ...where I got the above mentioned errors – Anonymous Aug 30 '20 at 07:04
  • The infinite loop is because your swap has no effect, so the array will never be sorted (if it wasn't sorted to start with). – khelwood Aug 30 '20 at 07:04
  • @Khelwood Okay ..but why isn't it working, I didn't send its value right...rather I used reference of object. – Anonymous Aug 30 '20 at 07:07
  • @Anonymous Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) --- Please read: [Can I ask only one question per post?](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) – Turing85 Aug 30 '20 at 07:08
  • You `swap` method successfully swaps the values held in `Example a` and `b`, but that has no effect on your array. Putting an int in a class does not make that int a reference to the place you copied it from. – khelwood Aug 30 '20 at 07:13
  • Please include your input and the output you get from it. – NomadMaker Aug 30 '20 at 07:26

1 Answers1

0

What you're doing with Example will not work. When you do

Example a = new Example(arr[i-1]);

Your Example a only contains the value of arr[i-1]. Altering a.a has no effect on your array. So your array never becomes sorted and you have an infinite loop.

If you want a method to swap elements in your array, the easiest way is to pass the array to the method.

static void swap(int[] arr, i, j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

Then you can swap elements with

swap(arr, i-1, i);
khelwood
  • 55,782
  • 14
  • 81
  • 108