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;
System.out.println(vect.get(i));
}
}
}
}``````

## All 16 Replies

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

}
}
}``````

Ohhh ok thank you so very much. I see the problem now.

I have one more question. 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.

You could use an if to detect the number, the question is where would you save the count.
It depends on the range of numbers. If a small range you could use an array.

You could use an if to detect the number, the question is where would you save the count.
It depends on the range of numbers. If a small range you could use an array.

Well the range of numbers are 1-99

You could use an array indexed by the number.

I'm sorry I am sort of a beginner. How would I go about implementing that into my code?? Sorry again.

create an array of ints with 99 elements.
newArray[foundNumber-1] += 1;

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

}
}
}``````

how to iterate it into the vector.

Can you explain?
Are trying to copy array contents to the vector?

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

//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
}
}``````

Well I'm trying to get the array to count the numbers of 1's, 2's, 3's.. and so on until I reaches 99.

we already suggested (at least) two ways to do so.
what have you tried, how have tried it.

show us the code in which you tried it and tell us what it's doing wrong

I didn't see zeroliken's post before I made that last post. But I am trying it now.

Be a part of the DaniWeb community

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