1,105,585 Community Members

Sorting my vector

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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));
               } 
        }
}
}
Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
1
 

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

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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));
               } 
        }
}
}
Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

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

        }
    }
    }
Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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.

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

You could use an array indexed by the number.

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,343 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 777 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

create an array of ints with 99 elements.
iterate over your vectorf:
newArray[foundNumber-1] += 1;

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

       
        }
    }
    }
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

how to iterate it into the vector.

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

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
1
 

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 
                }
            }
Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
stultuske
Posting Expert
5,343 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 777 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Member Avatar
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: