Modification of Bubblesort program with user input

T.Doe

I have created a program previously using the BubbleSort method that works to sort numbers in a list that already exists, however, I am having difficulty with trying to manipulate this program in order to allow a user to input the list of numbers to be sorted instead. So far I have:

import java.util.Scanner;

public class MedianValue {

public static void main(String[] args) {

    //use scanner to input list of numbers to sort

    Scanner scan = new Scanner(System.in);

    int[] numbers = new int[] {scan.nextInt()};


    //nested for loop
    //outer loop just iterating
        //inner loop going through and flipping
            //checking if out of order (if statement)


    int counter = 0;

    //outer loop: keep doing this until it's sorted

    for(int i = 0; i < numbers.length - 1; i = i + 1)

    //put in a inner loop number.length times minus one because we don't want to swap the last element

        for(counter = 0; counter < numbers.length - 1; counter = counter + 1)
        {



            if (numbers [counter] > numbers [counter + 1])
            {


                int temporary = numbers [counter];
                numbers [counter] = numbers [counter + 1];
                numbers [counter + 1] = temporary;
            }
        }


        for(int i =0; i < numbers.length; i = i + 1)
        {
            System.out.print(numbers[i] + " ");
        }
    }

}

But, in this program, instead of sorting the inputted numbers, the program simply prints the first number that is inputted by the user. I am not sure if I need to move where my scanner function is placed, or add on to it within the loop for it to sort all of the numbers as I want it to do. I am lost on where to change the program if that is the case.

ytobi

That's because int[] numbers = new int[] {scan.nextInt()}; is a single assigment. scan read a single input and assign to number[0].

You actually need to modify your code for scan to read n numbers and store in n-sized numbers.

something like.

int[] numbers = new int[scan.nextInt()];
for( int i = 0; i < numbers.length; i++)
   numbers[i] = scan.nextInt();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Perl script: modification to avoid necessity of user input

From Dev

User input to repeat program in Java

From Dev

Ending a python program with user input

From Dev

Processing user input in a python program

From Dev

Java program not reading user input

From Dev

matrices program asking for user input

From Dev

Taking in a user input to load a program

From Dev

Broken bubblesort program errors. UPDATED CODE

From Dev

C program skipping over user input?

From Dev

Java simple program not allowing to take user input

From Dev

Testing program that runs with user input from the console

From Dev

Keep running program until user input exit

From Dev

How to make a powershell program restart on user input

From Dev

C Program file-io user input

From Dev

Using Classes and methods with user input for an interactive program

From Dev

Why does the program not get input from the user?

From Dev

java simulate user input to external program

From Dev

java program to encode and decode user input

From Dev

Start a program after waiting for user input

From Dev

java Program does not wait for the user input

From Dev

How to run a local program with user input in Perl

From Dev

Processing user input in simple Python program

From Dev

Foolproof user input program using scanner

From Dev

Unable to get program to use user input

From Dev

Why is my program not reading the user input?

From Dev

Java program stuck in user input loop

From Dev

How to open a program using user input

From Dev

Need Python to register user input in my program

From Dev

Java simple program not allowing to take user input