## Featured Replies in this Discussion

- ... you can use the collections class to sort out your vector like so: ... and to display the how many 1's 2's etc i would just check the values of the sorted vector and add them to an array of size 99, and keep adding each index of the array as you find a match to keep a running total... because then your array at 0 would be for all the 1's and array…
- by stultuske
- by zeroliken

Hey, I was having trouble making my vector print out randomly generated numbers in ascending order. Can I please get some help?.

This is my assignment "Write a program name count.java that will use a Vector to store 10,000 randomly generated numbers (ranging from 1 to 99)

Now sort the vector in ascending order. Also, count the amount of 1's, 2's, 3's ... 98's and 99's in the vector.1. The program should display the sorted vector on the screen.

2. The program should also display the amount of 1's, 2's, ..... etc. on the screen. "

And this is the code I have so far.

`import java.util.*; import java.util.Vector; import java.util.Random; import javax.swing.JOptionPane; public class Count { public static void main(String[] args) { String output; Vector<Integer> vect = new Vector<Integer>(10000); { Collections.sort(vect); Random generator = new Random(); for (int i = 0; i < 10000; i++) { int rand = generator.nextInt(98)+1; vect.add(rand); vect.add(Math.abs(generator.nextInt()) % 100); System.out.println(vect.get(i)); } } } }`

you can use the collections class to sort out your vector like so:

```
Collections.sort(vect);
for(int i=0; i<vect.size(); i++) {
System.out.println(vect.get(i));
}
```

and to display the how many 1's 2's etc i would just check the values of the sorted vector and add them to an array of size 99, and keep adding each index of the array as you find a match to keep a running total... because then your array at 0 would be for all the 1's and array at 1 for all the 2's you can keep the total running in the array maybe by:

`array[0]=array[0]+1; //that will keep the running total for the 1's...`

but you wouldn use 0 you would iterate it through in a for statement, and keep a variable that will have the current value that is being checked so that when all the 1's are finished then you can increment your index and so on

Now its saying "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>

at count.Count.main(Count.java:26)

Java Result: 1"

The error is on this line

`System.out.println(vect.get(i));`

and here is my code now

```
package count;
import java.util.*;
import java.util.Vector;
import java.util.Random;
import javax.swing.JOptionPane;
import java.util.Collections;
import java.util.List;
public class Count {
public static void main(String[] args) {
String output;
Vector<Integer> vect = new Vector<Integer>(10000);
{
Collections.sort(vect);
Random generator = new Random();
for (int i = 0; i < vect.size(); i++);
{
int rand = generator.nextInt(98)+1;
vect.add(rand);
vect.add(Math.abs(generator.nextInt()) % 100);
System.out.println(vect.get(i));
}
}
}
}
```

Now its saying "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>

at count.Count.main(Count.java:26)

Java Result: 1"The error is on this line

`System.out.println(vect.get(i));`

and here is my code now

`package count; import java.util.*; import java.util.Vector; import java.util.Random; import javax.swing.JOptionPane; import java.util.Collections; import java.util.List; public class Count { public static void main(String[] args) { String output; Vector<Integer> vect = new Vector<Integer>(10000); { Collections.sort(vect); Random generator = new Random(); for (int i = 0; i < vect.size(); i++); { int rand = generator.nextInt(98)+1; vect.add(rand); vect.add(Math.abs(generator.nextInt()) % 100); System.out.println(vect.get(i)); } } } }`

your sort is trying to sort vect when it hasnt even been filled with numbers?:

```
public static void main(String[] args) {
String output;
Vector<Integer> vect = new Vector<Integer>(10000);
{
Random generator = new Random();
for (int i = 0; i < 10000; i++) {
int rand = generator.nextInt(98) + 1;
vect.add(rand);
vect.add(Math.abs(generator.nextInt()) % 100);
System.out.println(vect.get(i));
}
//you can only sort it after you generated the random numbers
Collections.sort(vect);
for (int i = 0; i < vect.size(); i++) {
System.out.println(vect.get(i));
}
}
}
}
```

I created the array but now I'm stuck on how to iterate it into the vector.

```
package count;
import java.util.*;
import java.util.Vector;
import java.util.Random;
import javax.swing.JOptionPane;
import java.util.Collections;
import java.util.List;
public class Count {
public static void main(String[] args) {
String output;
int[] anArray;
int a;
Vector<Integer> vect = new Vector<Integer>(1,10000);
{
Random generator = new Random();
for (int i = 1; i < 10000; i++) {
int rand = generator.nextInt(97);
vect.add(rand);
vect.add(Math.abs(generator.nextInt()) % 99);
}
//you can only sort it after you generated the random numbers
Collections.sort(vect);
for (int i = 1; i < vect.size(); i++) {
System.out.println(vect.get(i)+ 1);
}
anArray = new int[99];
}
}
}
```

What method would I use in order to count the numbers in the output... Like how many 1's and how many 2's etc. etc.

Wouldn't it be much easier if you add 1 to a counter exactly after the generated number

```
//your number generator code
for (int i = 1; i < 10000; i++) {
int rand = generator.nextInt(97);
vect.add(rand);
vect.add(Math.abs(generator.nextInt()) % 99);
//use a loop will add number instance to a counter
for (int j = 1; j < 99; j++) {//iterate 1-99 for the array's index
if(vect.get(i)==j)//once found
//you add 1 to the array with the same index as j... check stultuske's post
}
}
```