We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sorting my vector

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));
               } 
        }
}
}
5
Contributors
16
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
21
Views
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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));
               } 
        }
}
}

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

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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));
               } 
        }
}
}
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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));
            }

        }
    }
    }
DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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

drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You could use an array indexed by the number.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

       
        }
    }
    }
drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

how to iterate it into the vector.

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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 
                }
            }
zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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.

drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

drico7041
Newbie Poster
13 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1549 seconds using 2.82MB