Results 1 to 4 of 4
  1. #1
    k_seno is offline Member
    Join Date
    May 2013
    Rep Power

    Default Need a little help with my assignment

    Hi! My prof asks us to make a program that would generate 500 random numbers from 0-250 and store them in a "text file". It should then read the keys from the "text file" and store them in an array/vector, removing any duplicates(In other words if the number 55 is generated twice, only one of these 55ís should be added to the array).Now, the remaining keys must be written in another "text file", the keys must be sorted from the smallest number on the first line and the largest numbers on the last line.

    I have already made my code but can't seem to make it work. any help would be greatly appreciated.

    Java Code:
    import java.util.Vector;
    import java.lang.String;
    public class MainProg {
    	static GenKeys generateNumber = new GenKeys();
    	public static void main(String[] args) {
    	MainProg test = new MainProg();
    	public static void write(){
    			BufferedWriter out = new BufferedWriter(new FileWriter("keys.txt"));
    			for(int a = 1; a <= 10; a++){
    				 int keys = generateNumber.random();
    				out.write(keys + System.lineSeparator());		
    		} catch(IOException e){
    	public static Vector read(){
    		Vector<Integer> vector = new Vector<Integer>();
                FileReader fr = new FileReader("keys.txt"); 
                BufferedReader in = new BufferedReader(fr);
                for(int a = 0; a < 10; a++){
                	 //stores it in a Vector
                	for(int b = 0; b <= vector.size(); b++){
                		//checks in case there is a duplicate number
            }catch(IOException e){
                System.out.print("Non-Existant File");
    		return vector;  		
    	public void sorter(){
    		Vector<Integer> vector = read();
    		int size = vector.size();
            for (int j=1; j < size; j++) { 
                int temp =  vector.get(j); 
                int i = j-1;
                while(i >= 0 && vector.elementAt(i) > temp){
                    i = i-1;
                 System.out.print("After pass " + j +": ");
            System.out.print("Final Array: ");
    	public void printArray(){
    		Vector<String> vector = read();
            for(int x=0; x < vector.size(); x++){
                System.out.print(" " + vector.elementAt(x));
    the errors that comes up is this:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 > 0
    at java.util.Vector.insertElementAt(Unknown Source)
    at java.util.Vector.add(Unknown Source)
    at MainProg.main(

  2. #2
    k_seno is offline Member
    Join Date
    May 2013
    Rep Power

    Default Re: Need a little help with my assignment

    any help please?

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Rep Power

    Default Re: Need a little help with my assignment

    Ye Sorry it was late, i miss read (a,xxxx) as (a,.xxx) my bad.
    Last edited by monkeyjr97; 05-05-2013 at 12:08 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: Need a little help with my assignment

    Quote Originally Posted by monkeyjr97 View Post
    wouldn't the "," cause an error? or am i just being stupid here.
    Why would it cause an error? It is separating the parameters to the method call.

    As for the OP error: when you have 5 elements in a Vector you cannot insert an element at index 20. You can only insert an element at an index that is already occupied. From your error message it appears you are trying to insert an element at index 1 when the Vector is empty. In that case the only place you can insert is at index 0.

Similar Threads

  1. assignment help :L
    By Mike09lfc in forum New To Java
    Replies: 2
    Last Post: 12-03-2011, 02:38 AM
  2. Assignment Help
    By Albany in forum New To Java
    Replies: 6
    Last Post: 12-02-2011, 11:39 PM
  3. assignment help
    By xyknight in forum New To Java
    Replies: 6
    Last Post: 03-13-2011, 07:19 PM
  4. assignment help
    By esallender in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 12:10 PM
  5. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts