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]);
	}
 }
}

Look at line 19. Do you see the problem? numb = num[g];

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)

I assigned the values on line 19

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;

O thank you, I did not know that made a difference.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.