0

I'm a beginner at java and I have a hopefully simple question. I can get the numbers in order, but only a certain amount of numbers are added to the array. When there is a number less than the smallest number, it replaces that smallest number (the previous smallest gets removed from array). What can I do so that all of the array spots can be filled in?

public class apples {
    public static void main(String[] args) {
        System.out.print("How many numbers: ");
        int num=IO.readInt();
        double array[]=new double[num];
        double temp;
        boolean fixed=false;

        while (fixed==false){
            fixed=true;

            for(int j=0; j<num;j++){
                double x = IO.readDouble();
                array[j] = x;

                for (int i = 0; i < ( num - 1 ); i++) {
                    for (j = 0; j < num - i - 1; j++) {
                        if (array[j] > array[j+1]) {
                            temp = array[j];
                            array[j] = array[j+1];
                            array[j+1] = temp;
                            fixed=false;
                         }
                    }
                }

                System.out.println("Sorted list of integers:");
                for (int i = 0; i < num; i++) 
                    System.out.println(array[i]);
            }
        }
    }
}
1
  • The loop you're using to read the numbers to be sorted reads only one number before your sorting algorithm begins. Commented Mar 23, 2015 at 1:43

3 Answers 3

2

First read in the numbers, then sort the array (and it's an array of double not int). Something like,

public static void main(String[] args) {
    System.out.print("How many numbers: ");
    int num = IO.readInt();
    double array[] = new double[num];
    for (int j = 0; j < num; j++) {
        System.out.printf("Please enter double %d:%n", j + 1);
        array[j] = IO.readDouble();
    }
    System.out.println("unsorted array: " + Arrays.toString(array));
    Arrays.sort(array); // <-- or however you want to sort the array.
    System.out.println("sorted array: " + Arrays.toString(array));
}
Sign up to request clarification or add additional context in comments.

Comments

0
public static void main(String[] args) {
        System.out.print("How many numbers: ");
        int num = IO.readInt();
        double array[] = new double[num];
        for (int i = 0; i < num; i++) {
            System.out.print("["+i+"]Please enter your double");
            array[i] = IO.readDouble();
        }

        //Sort
        double temp = 0;
        for (int i = 0; i < num - 1; i++) {
            for (int j = i + 1; j < num ; j++) {
                if (array[j] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                 }
            }
        }

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

Comments

0
import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
    Scanner sc =new Scanner(System.in);
    int n =sc.nextInt();
    int arr[]=new int [n];

    for (int i =0;i<n;i++){
        arr[i]=sc.nextInt();}

        Arrays.sort(arr);
        //if(n%2==0){median}
        
        System.out.println( Arrays.toString(arr));

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.