0
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> my_array = new ArrayList<Integer>();
        while (true) {
            System.out.println("Enter your input to add to the list. Type in '-1' to finish adding. ");
            int user_in = sc.nextInt();
            if (user_in == -1) {
                break;
            } else {
                my_array.add(user_in);
            }
        }
        System.out.println(sort(my_array));
    }
    public static ArrayList<Integer> sort(ArrayList<Integer> arr) {
        for (int i = 1; i < arr.size(); i++) {
            int key = arr.get(i);
            int j = i;
            while (j > 0 && arr.get(j-1) > arr.get(j)) {
                arr.set(j,arr.get(j-1));
                j--;
            }
            arr.set(j,key);
        }
        return arr;
    }
}

I tried running this program in my compiler and it won't work, I have no idea as to why because I double checked both the main method and the sort method that I created; the problem I have is that whenever the user tries to add an element to the ArrayList they will instantly lose their ability to enter the second input before the program closes and returns some value(in this case the arraylist). I don't understand why the program instantly finishes after the user inputs the first value because the while loop should make it continue if I'm not mistaken, so what am I doing wrong?

I tried running the program on multiple online compilers and visual studio code but to no avail, I even ran some tests with CHATGPT and the bot cannot find an issue with it which is why I'm confused. When I tried adding a continue statement in the while loop after the user inputs a value I got the same result.

ege.exe
  • 33
  • 7
  • I tried your program and the input works fine. It's not sorting correctly, but it reads number and it only stops when I enter -1. Are you sure you saved your file and you're compiling and running the correct one? – Federico klez Culloca Mar 27 '23 at 14:40
  • Focus on your use of the scanner and how you enter the data. I guess there is a delimiter character that tells the scanner the first int is read. But then you need to clear this input so the scanner can read the next int. See also https://stackoverflow.com/a/49153311/4222206 – Queeg Mar 27 '23 at 14:40
  • Thank you for clarifying @FedericoklezCulloca - I had a question about what you said about not sorting correctly; am I making a logical error? Can you explain how? But yes I tried emptying the input buffer using sc.nextLine() and it seems to have resolved the issue. I also opened and closed visual studio code and that seems to have worked somehow. – ege.exe Mar 27 '23 at 15:11
  • Thank you for providing the link for Scanner class problems @Quegg, it seems as though my issue was leaving a newline character after taking input when not calling sc.nextLine() after taking the input every time. The issue is solved now because I now understand how to clear input buffer. – ege.exe Mar 27 '23 at 15:14
  • Are you not allowed to use `Collections.sort(my_array);`? – DevilsHnd - 退職した Mar 27 '23 at 15:50
  • No @DevilsHnd, I was not allowed to use any other prebuilt methods to solve this problem; however, I will keep the method you provided in mind the next time I try to solve a problem where I'm permitted to use other methods. – ege.exe Mar 27 '23 at 16:02

0 Answers0