1

I am trying to create a program that sorts user inputted integers from greatest to least. Also I need to find a way to print the maximum and minimum numbers. The code was sorting fine when I had defined values but now that I have switched it to user input it sends back "0"s for some reason. This is my code

import java.util.Scanner;
public class SortInteger {

        public static void main(String [] args)
        {
            Scanner input = new Scanner(System.in); 
            System.out.println("Please input three numbers");
            int num = input.nextInt();
            int number [] = new int [num];  //Sorting Array
            int temp;

            boolean correct = false; // Forces the sorting to continue till the numbers are in order

            while(correct ==false){
                correct = true;
            for(int i = 0; i>number.length-1; i++ ){

                if(number [i] >  number [i+1]){
                    temp = number [i+1];
                    number [i+1] = number[i];
                    number[i]= temp;
                    correct = false;

                }
            }
        }
                for(int i = 0; i<number.length-1; i++){   //outputs the array to user
                    System.out.println(number[i]);

                }

        }

    }
1
  • 1
    i>number.length-1; should have been i<number.length-1; Commented Feb 20, 2013 at 17:33

3 Answers 3

2

you have taken only one number

int num = input.nextInt();

and you are using it for array size :

     int number [] = new int [num];

but in rest of the code you haven't taken any input so your array is empty.

Code::

import java.util.*;
class test{
 public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in); 
        System.out.println("Please input three numbers");
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        int num3 = input.nextInt();
    int number [] = {num1,num2,num3};  //Sorting Array
    int temp;

    boolean correct = false; // Forces the sorting to continue till the numbers are in order

    while(correct ==false){
        correct = true;
    for(int i = 0; i<number.length-1; i++ ){

        if(number [i] >  number [i+1]){
            temp = number [i+1];
            number [i+1] = number[i];
            number[i]= temp;
            correct = false;

        }
    }
}
        for(int i = 0; i<number.length; i++){   //outputs the array to user
            System.out.println(number[i]);

        }

}}

Output::

Please input three numbers
1
5
4
1
4
5
Sign up to request clarification or add additional context in comments.

2 Comments

So what is the best way to fix this im pretty new to programming
oh no loop . you need only three numbers.
2

you are only initializing your array. you never initialized your array with elements, thus your array elements get default values.

        System.out.println("Please enter size");
        int num = input.nextInt();
        int number [] = new int [num];  //Sorting Array

         for(int i=0; i<number.length; i++){
               System.out.println("Please enter element at index " + i);
              number[i] = input.nextInt()
         }

2 Comments

Tried this solution it takes the input correctly but fails to sort them in the proper order this seems like a very complex program to just sort three numbers in order
Nevermind it sorts correctly if you add an extra element. So if you want it to sort three values if you add four values it sorts the three correctly
1

As others pointed out you need to populate your array with multiple values, currently you are requesting only one int.

Also, are you sure that works? Should't your first for loop read:

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

Instead of:

for(int i = 0; i>number.length-1; i++ )

On a side note, it looks to me that your sorting algorithm is O(n2) you may want to look into mergesort and quicksort.

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.