Results 1 to 4 of 4
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation need help with IndexOutOfBounds error

    Hello forum

    what im trying to do is read in candidates and parties from a file into an ArrayList<Seat> which has a ArrayList<Party> in it. But it keeps throwing this IndexOutOfBoundsException :confused:. The strange thing is when i create a local ArrayList<Party> in class Election and add it to that it works fine so what am i missing .
    Heres my election2 class
    Java Code:
    import java.util.*;
    import java.util.regex.*;
    import java.io.*;
    /*
     * This is a remake of the Election class
     * The difference it that this uses ArrayList
     * rather than the inflexible Array.
     * Yeah yeah 
     * 
     * */
    
    public class Election2 
    {
    	//public attributes
    	public static final String REGEX = ":";
    	//private attributes
    	/*sL stands for seatList*/
    	private ArrayList<Seat> sL = new ArrayList<Seat>(); 
    	private int numOfSeat;
    	private int numberOfCand;
    	private int voteCast;
    	
    	//class constructors
    	public Election2()
    	{
    		
    	}
            //class methods 
             public void seatFileReadIn()
    	{
    		try
    		{
    			int seatCount = 0;
    			/*Create an Scanner object to read in seatFile*/
    			Scanner seatFileInput = new Scanner(new FileInputStream("seatFile"));
    			/*checks if the file has input if not exits*/
    			if(!seatFileInput.hasNext())
    			{
    				System.out.printf("FATAL ERROR Cannot read seatFile exiting System\n");
    				System.exit(1);
    			}
    			/*Sets number of seats*/
    			setNumOfSeat(Integer.parseInt(seatFileInput.nextLine()));
    			/*adding names of seats and number of voter on roll*/
    			while(seatFileInput.hasNext())
    			{
    				String seatline = seatFileInput.nextLine();
    				String [] seatParts = seatline.split(" ");
    				getsL().add(new Seat(seatParts[0],Integer.parseInt(seatParts[1])));
    				++seatCount;
    			}
    			/*check number of seats read in not higher the getNumOfSeat variable*/
    			if(seatCount > getNumOfSeat())
    			{
    				System.out.printf("Fatal Error seatCount > number of Seat allowed\n");
    				System.exit(1);
    			}
    			
    			seatFileInput.close();/*closing file*/
    			
    			}
    		catch(FileNotFoundException fnfe)
    		{
    			System.out.printf("File not found in class Election " +
    			"seatFileReadIn()\n");
    			System.out.println(System.getProperty("user.dir"));
    			System.exit(1);
    		}
    		catch(Exception e)
    		{
    			System.out.printf("Something wrong in class Election " +
    			"seatFileReadIn()\n");
    			System.exit(1);
    		}
    		
    	}//end of seatFileReadIn()
    	//---------------------------
    	public void cndFileReadIn()
    	{
    		try
    		{
    			int cndCount = 0;
    			/*getting candidates/party in cnd file*/
    			Scanner cndFileInput = new Scanner(new FileInputStream(getsL().get(cndCount).getSeatName() +".cnd"));
    			/*checks if the file has input*/
    			if(!cndFileInput.hasNext())
    			{
    				System.out.printf("FATAL ERROR Cannot no data in cndFile exiting System\n");
    				System.exit(1);
    			}
    			/*setting number of candidates*/
    			setNumberOfCand(Integer.parseInt(cndFileInput.nextLine()));
    			/*add candidates and party to list*/
    			
    			while(cndFileInput.hasNext())
    			{
    				
    				String cndLine = cndFileInput.nextLine();
    				String [] cndParts = cndLine.split(" ");
    				getsL().get(cndCount).addParties(cndCount+1,cndParts[0],cndParts[1]);
    				++cndCount;
    			}
    			
    			/*Check if number of candidate does not exceed num read
    			 * into getNumberOfCand */
    			if(cndCount > getNumberOfCand())
    			{
    				System.out.printf("Fatal Error cndCount > number of candidates allowed\n");
    				System.exit(1);
    			}
    			
    			
    		}//end of try
    		catch(FileNotFoundException fnfe)
    		{
    			System.out.printf("File not found in class Election " +
    			"cndFileReadIn()\n");
    			System.exit(1);
    		}
    		/*catch(Exception e)
    		{
    			System.out.printf("Something wrong in class Election " +
    			"cndFileReadIn()\n");
    			System.exit(1);
    		}*/
    	}//end of cndFileReadIn()
    	//---------------------------
    	public void votFileReadIn(String votFileName)
    	{
    		try
    		{
    			/*Opening file*/
    			Scanner votFileInput = new Scanner(new FileInputStream(votFileName+".vot"));
    			
    			/*checks if the file has input*/
    			if(!votFileInput.hasNext())
    			{
    				System.out.printf("FATAL ERROR Cannot no data in cndFile exiting System\n");
    				System.exit(1);
    			}
    			/*Creating listIterator*/
    			ListIterator<Seat> vli = getsL().listIterator();
    			Seat vot = null;
    			/*reading in actual votes cast*/
    			setVoteCast(Integer.parseInt(votFileInput.next()));
    			
    			/*using class Pattern for regular expression*/
    			Pattern colon = Pattern.compile(REGEX);
    			/*reading in votes*/
    			while(vli.hasNext())
    			{
    				while(votFileInput.hasNext())
    				{
    					String votLine = votFileInput.nextLine();
    					Matcher matches = colon.matcher(votLine); 
    					vot.voteRead(votLine,getNumberOfCand());
    				  if(matches.matches())
    				  {
    					votFileInput.skip(votLine);
    				  }
    				}
    			}
    			/*checking votecast doesnt exceed numbers on roll*/
    			vli = getsL().listIterator();
    			while(vli.hasNext())
    			{
    				if(getVoteCast() > vot.getNumOnRoll())
    				{
    					System.out.printf("Fatal Error voteCast > number on roll\n");
    					System.exit(1);
    				}
    			}
    		}//end of try
    		catch(FileNotFoundException fnfe)
    		{
    			System.out.printf("File not found in class Election " +
    			"cndFileReadIn()\n");
    			System.exit(1);
    		}
    		catch(Exception e)
    		{
    			System.out.printf("Something wrong in class Election " +
    			"cndFileReadIn()\n");
    		}
    	}//end of votFileReadIn()
    	//--------------------------
    	public void displaySeatResult()
    	{
    		/*System.out.printf("Preferential voting seat %s\n");
    		System.out.printf("Reading and validating votes.....\n");
    		setGovernment();
    		System.out.printf("Counting primary votes : %d candidates standing \n",seats[index].getNum());
    		System.out.printf("Electors on Rolls: %d \n",getNumberOnRoll());*/
    	}
    	
    	//get set methods
    	private void setsL(ArrayList<Seat> sL) 
    	{
    		this.sL = sL;
    	}
    
    	public ArrayList<Seat> getsL() 
    	{
    		return this.sL;
    	}
    	//--------------------------------------
    	public void setNumOfSeat(int numOfSeat)
    	{
    		this.numOfSeat = numOfSeat;
    	}
    	public int getNumOfSeat() 
    	{
    		return this.numOfSeat;
    	}
    	//--------------------------------------------
    	public void setNumberOfCand(int numberOfCand) 
    	{
    		this.numberOfCand = numberOfCand;
    	}
    	public int getNumberOfCand() 
    	{
    		return this.numberOfCand;
    	}
    	//-----------------------------------------------
    	public void setVoteCast(int voteCast) 
    	{
    		this.voteCast = voteCast;
    	}
    	public int getVoteCast() 
    	{
    		return this.voteCast;
    	}
    	//----------------------------------------------
    	
    	
    	
    	//toString & equals
    }
    This is the line thats giving me trouble
    Java Code:
    getsL().get(cndCount).addParties(cndCount+1,cndParts[0],cndParts[1]);
    This is the method that im trying to access in my SeatList
    Java Code:
    public void addParties(int partyNum,String candidate,String party)/*Adds parties and candidates to an aL parties*/
    	{
    		getParties().add(new Party(partyNum,party,candidate));
    	}
    Any help would be much appreciated and thank you in advance
    Last edited by ShinTec; 04-29-2010 at 08:20 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Print out the lengths of all the arrays and sizes of all the lists that are affected
    by the line getsL().get(cndCount).addParties(cndCount+1,cndPar ts[0],cndParts[1]); together with the value of cndCount before that line is executed.
    You should then be able to work out why you are accessing out of bounds.

  3. #3
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    Figured it out eh for some whacked out reason it wasnt seeing the cndCount so i just added int i and swap it with the cndCount within the while and it worked;)
    before
    Java Code:
    	while(cndFileInput.hasNext())
    			{
    				String cndLine = cndFileInput.nextLine();
    				String [] cndParts = cndLine.split(" ");
    				getsL().get(cndCount).addParties(cndCount+1,cndParts[0],cndParts[1]);
    				++cndCount;
    			}
    after
    Java Code:
    while(cndFileInput.hasNext())
    			{
    				
    				int i =0;
    				String cndLine = cndFileInput.nextLine();
    				String [] cndParts = cndLine.split(" ");
    				getsL().get(i).addParties(cndCount+1,cndParts[0],cndParts[1]);
    				i++;
    				++cndCount;
    			}

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You realize of course that that translates to getsL().get(0)... every time?

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  2. ArrayList IndexOutOfBounds... error
    By Arius in forum New To Java
    Replies: 7
    Last Post: 01-17-2010, 11:15 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 09:53 AM
  5. Confusing IndexOutOfBounds Error?
    By tibbyuk in forum New To Java
    Replies: 2
    Last Post: 08-08-2008, 11:42 PM

Posting Permissions

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