Hey everyone, I am writing a program that needs to take 10 integers and display them, then it needs to sort them in order from lowest to highest and then display them again. I was trying to use selection sort to sort them, but I think I set it up wrong. My code compiles and runs fine until it prints the numbers. Alls it prints is a bunch of zeros, I am not really sure what is wrong with it. Any help is greatly appreciated, thanks in advance.

import TerminalIO.KeyboardReader;
public class sort
{
 public static void main(String[] args)
 {

	KeyboardReader reader = new KeyboardReader();
	int[] num = new int[10];
	int numb;
	int t;
	int u;
	int min;
	int temp = 0;
	
	System.out.print("Enter your numbers: ");
	for(int g = 0; g < 10; g++)
	{
	  numb = reader.readInt();
	  numb = num[g];
	}

	for(int y = 0; y < 10; y++)
	{
	  System.out.println("The numbers you entered are" +num[y]);
	}

	for (int i = 0; i < 10; i++)
	{
	  min = i;
	  for(int a = 0; a < 10; a++)
	  {
	  	if(num[i] < num[min]);
		{
		  if (min != i)
		  {
		  	num[i] = num[min];
			num[min] = temp;
		
		  }
		}
	   }
	}
	for(int h = 0; h < 10; h++)
	{
	  System.out.print(num[h]);
	}
 }
}

The best way to solve something like this is to work backwards from where you know the problem exists.

for(int h = 0; h < 10; h++)
	{
	  System.out.print(num[h]);
	}

It's pretty obvious that this is okay. It walks the array for values [0..9] and prints each. If you say you're getting zeros in each place, how did they get there? Where did you assign values to these ten variables, and what values did you assign?

(you might want to fix that print loop - it'll print out something like
3445566778899011125499000, when you might want 34 45 56 67 78 89 90 111 254 99000)

Edited 5 Years Ago by jon.kiparsky: n/a

for(int g = 0; g < 10; g++)
	{
	  numb = reader.readInt();
	  numb = num[g];
	}

What happens in this loop?
You have three variables: two ints (g and numb) and an array of ints (num). What are their values coming in to the loop, what happens to each of them on each pass through the loop, and what are their values after the loop executes?

I assigned the values on line 19

Yes, but your terms were reversed. You should have done this: num[g] = numb;

The = operator is not an assertion (a is equal to b), it's an imperative (set a to the value of b). The former sense is what you might expect from math, but it's incorrect.

This question has already been answered. Start a new discussion instead.