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

    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.io.*;
    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();
    		write(); 
    		read();
    		
    	}
    	
    	public static void write(){
    		
    		
    		try{	
    			BufferedWriter out = new BufferedWriter(new FileWriter("keys.txt"));
    			
    			for(int a = 1; a <= 10; a++){
    				 int keys = generateNumber.random();
    				out.write(keys + System.lineSeparator());		
    			}
    			out.close();
    			
    		} catch(IOException e){
    			
    		}			
    	}
    	
    	public static Vector read(){
    		
    		Vector<Integer> vector = new Vector<Integer>();
    		
            try
            {  	
                FileReader fr = new FileReader("keys.txt"); 
                BufferedReader in = new BufferedReader(fr);
                for(int a = 0; a < 10; a++){
                	 //stores it in a Vector
                	vector.add(a,in.read());
                	for(int b = 0; b <= vector.size(); b++){
                		//checks in case there is a duplicate number
                		if(vector.elementAt(a).equals(vector.elementAt(b))){
                			vector.remove(a);
                		}
                	}
                	
                } 
                in.close();
     
            }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){
                    vector.add(i+1,vector.elementAt(i));
                    i = i-1;
                 }
                 vector.add((i+1),temp);
                 System.out.print("After pass " + j +": ");
                 printArray();
            } 
            System.out.print("Final Array: ");
            printArray();
        }
    	
    	public void printArray(){
    		Vector<String> vector = read();
            for(int x=0; x < vector.size(); x++){
                System.out.print(" " + vector.elementAt(x));
            }
            System.out.println();
        }	
    }
    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.read(MainProg.java:43)
    at MainProg.main(MainProg.java:12)

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

    Default Re: Need a little help with my assignment

    any help please?

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

    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 01:08 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    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, 03:38 AM
  2. Assignment Help
    By Albany in forum New To Java
    Replies: 6
    Last Post: 12-03-2011, 12:39 AM
  3. assignment help
    By xyknight in forum New To Java
    Replies: 6
    Last Post: 03-13-2011, 08:19 PM
  4. assignment help
    By esallender in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 01:10 PM
  5. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 10: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
  •