# Sorting Numbers

Page 1 of 1

## 9 Replies - 4061 Views - Last Post: 13 January 2010 - 09:36 AMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=149664&amp;s=b0a100da38441e1ee7af5baab2909ad8&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hlln

Reputation: 5
• Posts: 89
• Joined: 12-July 09

# Sorting Numbers

Posted 13 January 2010 - 12:20 AM

I am stuck on this assignment my code is real close to the answer but i do not know how to fix it.

Lab Goal : This lab was designed to teach you more about sorting.

Lab Description : Send in a number and get back an array that contains all of the digits from the number in sorted order. 634152 would come back [1, 2, 3, 4, 5, 6]. You may not use Strings in this program.

You must use % and /. 456 % 10 = 6 456 / 10 = 45
You must use % and /. 102 % 10 = 2 102 / 10 = 10
You must use % and /. 9123 % 10 = 3 9123 / 10 = 912
You must use % and /. 9 % 10 = 9 9 / 10 = 0

Sample Data :
567891
901912468
864213507

Sample Output :
1 5 6 7 8 9
0 1 1 2 4 6 8 9 9
0 1 2 3 4 5 6 7 8

```//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
//instance variables and other methods not shown

private static int getNumDigits(int number)
{
int count = 0;
for(int i=10; i<=number; i=i*i)
{
count++;

}

return count;
}

public static int[] getSortedDigitArray(int number)
{
int [] a= new [count]
for(int i=10; i<=100000; i=i*i)
{
a+={number/i%10};

}

}
}
```

```//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import static java.lang.System.*;

public class Lab15e
{
public static void main(String args[])
{
//test case 1
int[] one = NumberSort.getSortedDigitArray(567891);
for(int item : one)
{
System.out.print(item + " ");
}
System.out.println();

//test case 2
int[]two= NumberSort.getSortedDigitArray(901912468);
for(int item : two )
{
out.print(item+ " ");
}
out.println();
int[]three= NumberSort.getSortedDigitArray(864213507);
for(int item : three)
{
out.print(item + " ");
}

}
}
```

This post has been edited by hlln: 13 January 2010 - 12:21 AM

Is This A Good Question/Topic? 0

## Replies To: Sorting Numbers

### #2 hlln

Reputation: 5
• Posts: 89
• Joined: 12-July 09

## Re: Sorting Numbers

Posted 13 January 2010 - 12:57 AM

I got everything fixed BUT when i run there is no output. WHY? SEE ATTACHED!!

### #3 0x00hex

Reputation: 2
• Posts: 18
• Joined: 11-January 10

## Re: Sorting Numbers

Posted 13 January 2010 - 03:29 AM

Seems you have forgotten to include "System." in your loops when printing out the numbers.
```//Should be System.out.print(item + " ");
out.print(item + " ");

```

### #4 g00se

• D.I.C Lover

Reputation: 3533
• Posts: 16,009
• Joined: 20-September 08

## Re: Sorting Numbers

Posted 13 January 2010 - 04:25 AM

Make sure your NumberSort method is returning the array properly - sounds like it's empty

### #5 Simple_Condolences

Reputation: 11
• Posts: 180
• Joined: 10-January 10

## Re: Sorting Numbers

Posted 13 January 2010 - 06:32 AM

If that still doesn't fix it then you need to check your array and make sure that data is being stored though.. well, really, you'd know... you'd have an OutOfBoundsException at runtime... I dunnoooo!

- Zach

### #6 g00se

• D.I.C Lover

Reputation: 3533
• Posts: 16,009
• Joined: 20-September 08

## Re: Sorting Numbers

Posted 13 January 2010 - 07:08 AM

Quote

If that still doesn't fix it then

How would it? If that were a problem, it would be a compile time problem. It isn't, since System. is imported static

### #7 hlln

Reputation: 5
• Posts: 89
• Joined: 12-July 09

## Re: Sorting Numbers

Posted 13 January 2010 - 09:15 AM

```//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
//instance variables and other methods not shown
private static int getNumDigits(int number)
{
int count = 0;
for(int i=number; i>=0; i/=10)
{
number=number/=10;
count++;
}
return count;
}

public static int[] getSortedDigitArray(int number)
{
int [] a=new int [getNumDigits(number)];
for(int i=number; i>=0; i/=10)
{
a[i]=number%=10;

}
Arrays.sort(a);
return a;
}

}

```

```//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import static java.lang.System.*;

public class Lab15e
{
public static void main(String args[])
{
//test case 1
int[] one = NumberSort.getSortedDigitArray(567891);
for(int item : one)
{
System.out.print(item + " ");
}
System.out.println();

//test case 2
int[]two= NumberSort.getSortedDigitArray(901912468);
for(int item : two )
{
out.print(item+ " ");
}
out.println();
int[]three= NumberSort.getSortedDigitArray(864213507);
for(int item : three)
{
out.print(item + " ");
}
}
}

```

NO OUTPUT WHY?

### #8 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting Numbers

Posted 13 January 2010 - 09:32 AM

you got an infinite loop in your getNumDigits method..try to find it

### #9 hlln

Reputation: 5
• Posts: 89
• Joined: 12-July 09

## Re: Sorting Numbers

Posted 13 January 2010 - 09:34 AM

i can't find it all my numbers are going to stop at i>=0;
```//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
//instance variables and other methods not shown
private static int []a;
private static int getNumDigits(int number)
{
int count = 0;
for(int i=number; i>=0; i/=10)
{
number/=10;
count++;
}
return count;
}

public static int[] getSortedDigitArray(int number)
{
a=new int [getNumDigits(number)];
for(int i=number; i>=0; i/=10)
{
a[i]=number%=10;

}
Arrays.sort(a);
return a;
}

}

```

### #10 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Sorting Numbers

Posted 13 January 2010 - 09:36 AM

anyway here's the fixed program

```import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;

public class NumberSort
{
//instance variables and other methods not shown
private static int getNumDigits(int number)
{
int count = 0;
for(int i=number; i>0; i/=10)
{

count++;
}
return count;
}

public static int[] getSortedDigitArray(int number)
{
int [] a=new int [getNumDigits(number)];
for(int i= 0; i < a.length; i++)
{
a[i]=number%10;
number /= 10;

}
Arrays.sort(a);
return a;
}
}

```

look at it and try to understand the differences between my code and your code..