Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Array index out of bounds

  • 22-06-2012 11:24am
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭


    Could somebody explain why I am getting this error on the bolded line? The indices contain what I want.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package scjp;
    
    /**
     *
     * @author paul
     */
    import java.util.*;
    import java.io.*;
    
    class DVDInfo implements Comparable<DVDInfo>{
    	String title;
    	String genre;
    	String leadActor;
    	
    	DVDInfo(String t, String g, String a){
    		title = t; genre = g; leadActor = a;
    	}
    	
    	public String toString(){
    		return title + " " + genre + " " + leadActor + "\n";
    	}
    	
    	public int compareTo(DVDInfo d){
    		return title.compareTo(d.getTitle());
    	}
    	
    	public String getTitle(){
    		return title;
    	}
    	
    	public String getGenre(){
    		return genre;
    	}
    		
    	public String getLeadActor(){
    		return leadActor;
    	}
    	
    	public void setTitle(String t){
    		title = t;
    	}
    	
    	public void setGenre(String g){
    		genre = g;
    	}
    	public void setLeadActor(String l){
    		leadActor = l;
    	}
    	
    	
    }
    
    public class MyDvd{
    	public static void main(String[] args){
    		ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
    		populateList(dvdList);
    		
    		Collections.sort(dvdList);
    		System.out.println(dvdList);
    		
    	}
    	
    	public static void populateList(ArrayList<DVDInfo> dvdList){
    		try{	
    			File file = new File("C:\\dvdinfo.txt"); 
    			FileReader fr = new FileReader(file);
    			BufferedReader br = new BufferedReader(fr);
    		
    			String s;
    			while ((s = br.readLine()) != null){
    				String[]tokens = s.split("/");
    				[B]dvdList.add(new DVDInfo(tokens[0],tokens[1],tokens[2]));[/B]
                                    
    			}
    			br.close();
    		}catch(IOException e){System.out.println("File doesn\'t exist");}
    		
    	}
    	
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Put a breakpoint on s.split("/") and see if it returns an array of length 3? Never trust strings, always check array bounds.


  • Registered Users, Registered Users 2 Posts: 211 ✭✭CrazyFish


    Maybe post the text file of the dvd info. But I would imagine that one of the tokens is returning null. I would just put a print statement of the three tokens just before you add them and see which one is returning null.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    It's not that one of the tokens is null...

    The correct way to handle this is that you're expecting three token. Check that there are three tokens and if not, you've got an error to handle.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    Woops thanks, a stray ] moved to a new line somehow so there were not 3 tokens on the last line!

    Dint spot it because there is a ] in the last line of output. :/


Advertisement