error out of bound exception

two dimension array walking off end of array but I check it (if statem

Page 1 of 1

6 Replies - 1937 Views - Last Post: 08 July 2009 - 12:57 PM Rate Topic: -----

#1 smith123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-June 09

error out of bound exception

Posted 08 July 2009 - 09:13 AM


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import javax.swing.*;



public class MovieListDriver {
	
	
	
	
	
	static int choice = 1;
	static int count;
	static int row = 0;
	static int columns = 3;
	static String [] Records;
	static String [][] MovieData;
	static int j;
	static  int k;
	static Scanner keyboard = new Scanner(System.in);
	static StringTokenizer object;
	
	
				public static void main(String[] args) {
					String FieldName;
					String MovieRec;
					int pos1;
					 int pos2;
					 int pos3;
					 int pos4;
	
	
		MovieList movielist1 = new MovieList();
	
	
		FieldName = movielist1.getFieldNames();
		MovieRec = movielist1.getMovieRec();
		System.out.println(FieldName);
		System.out.println(MovieRec);
		
		/*
		StringTokenizer strTok1 = new StringTokenizer(MovieRec, "|", false);
		
		count = strTok1.countTokens();
		System.out.println("The number of records are" + count);
		
		StringTokenizer strTok = new StringTokenizer(FieldName, "~", false);
		System.out.println("Field Names are:");
		while ( strTok.hasMoreTokens()) {
			System.out.print(" \t"+ strTok.nextToken());
		}  // end while
			
		
			
			for ( j = 0; j < count; j++){
			Movie = MovieRec.split("[|]");
			System.out.print(Movie[ j ]);
		}
			
			// read string into double scripted array
			
				
				int count1 = 0;
				int pos1 = 0;
				int length = Movie.length;
				System.out.println("\n\nThe length is" +length);
				 // Movie1 = new String [ row][columns];
				 
				 //String(Movie1,10,length );
				StringBuffer Movie2 = new StringBuffer();
				
		
				
				
				int pos = 0;
				int startindex;
				
				for ( j = 0; j < Movie.length; j++){
						
						for ( k = 0; k < 3; k++) {
						pos = Movie[j].indexOf("~", 0);
						Movie1 [j] = Movie[j].split("~");
						pos1 = Movie[j].lastIndexOf("~");
						Movie1 [j ] [ k] = Movie [j].substring(pos, pos1)
						
						
						
						columns++;
						System.out.println(Movie1[j]);
					// Movie2.append(Movie1[j]);
					 
						 } // end inner for statement
						 
					 } // end outer for statement
					
			
				*/
			
				
				
				MovieRec = MovieRec.trim();
				MovieRec = MovieRec.substring(0);
			 Records = new String [ MovieRec.length()  ];
			 for (j = 0; j < MovieRec.length(); j++) {
				 Records  = MovieRec.split("|");
				 System.out.print(Records[j]);
			 }
/*
			k = 0;
				MovieData = new String [ Records.length - 1] [ 3 ];
				for ( j = 0; j < Records.length; j++ ) {
					pos1 = Records[ j ].indexOf("~");
					MovieData [ j ] [ 0 ] = Records[j].substring(0,pos1);
					pos2 = Records[ j ].indexOf("~");
					MovieData [ j ] [ 1 ] = Records[j].substring(pos1,pos2);
					pos3 = Records[j].indexOf("~");
					MovieData [ j ] [ 2 ] = Records[j].substring(pos2,pos3);
					
				}
					
					for ( j = 0; j < MovieData.length; j++ ) {
							for ( k = 0; k < 3; k++ ) {
								System.out.print(MovieData[j][k]);
							} // end inner for 
					} // end outer for	
	*/
	
						//	StringTokenizer strTok1 = new StringTokenizer(MovieRec, "|", false);
							
						/*
							j = 0;
							k = 0;
							MovieData = new String [ Records.length ] [ 3 ];
							for ( j = 0; j < Records.length; j++ ) {
							StringTokenizer strTok1 = new StringTokenizer(Records[j], "~");
								while ( strTok1.hasMoreTokens() ) {
								
								MovieData [ j ] [ k ] = strTok1.nextToken();
								j++;
								k++;
							}			
						}
								
							for ( j = 0; j < MovieData.length; j++ ){
								for ( k = 0; k < 3; k++ ) {
									System.out.print(MovieData[j][k]);
								} // end inner for
						} // end outer for 
							
	*/				
	
						MovieData = new String [ Records.length ][3];
						k = 0;
						for ( j = 0; j < Records.length; j++ ) {
						StringTokenizer object = new StringTokenizer(Records[j],"~");
					
						
							
						
						
						
						while(object.hasMoreTokens())  { // begin while
										
										MovieData [ j ] [ k ] = object.nextToken();
										k++; 
						}		// end while
															
					} // end for statement				
				
						
				
						
						for ( j = 0; j < Records.length; j++ ) {
							for ( k = 0; k < 3; k++ ) {
								System.out.print(MovieData[j][k]);
							} // end inner for statement
					}// end outer for statement
	
					
		
		
		while ( choice != 0 ) {// begin while
			System.out.print("\n\n 1.Display All records \n 2.Find Movies \n 3.Get Index\n");
			System.out.print(" 0. Please enter 0 to quit\n Enter your option");
			
			choice = keyboard.nextInt();  //static
			switch(choice)
			{
			case 0:
			break;
			case 1:
			diplayAll();
			break;
			case 2:
			findMovie();
			break;
			case 3:
			int index = getIndex();
			if(index == -1)
			System.out.print("Record Not Found");
			else 
			System.out.print("Record number:" +index);
			break;
			default:
			System.out.println("Invalid Option");
			}// end switch
			
		}  // end while
		
	} // end main
	
public static void diplayAll()
{
for(j = 0; j < count; j++){
	for( k = 0; k < 3; k++){
				System.out.println(MovieData[j][k]+ "\t");
	System.out.println();
		}
	}
}

public static void findMovie() 
{
int flag = 0;
System.out.println("Enter Movie Name to Find:");
String temp = keyboard.next();
for(j = 0; j < count; j++)
	{
	if(MovieData[j][0].contains(temp))
	{
	flag = 1;
	for(k = 0; k < 3; k++)
				System.out.print(MovieData[j][k]+" t");
		System.out.println();
		}
	}
if(flag == 0)
System.out.println("Record not Found");
}

public static int getIndex()
{
System.out.println("Enter Movie Name to Find INdex or (Record Number):");
String temp = keyboard.next();
for( j = 0; j < count; j++)
	{
	if(MovieData[j][0].contains(temp))
	return j;
	}
return -1;
}




}






I keep getting an array out of bound exceptions. I have looked at java.sun.com for this error. I think I am walking off the array, but I don't know why I am walking off the end of the array. The one dimensional array reads in fine but its walking off the two dimensional array.

Please see lines 155-180.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: error out of bound exception

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: error out of bound exception

Posted 08 July 2009 - 09:38 AM

Do you mind posting the exact error? That way we can see what line the error is on and where it's trying to access a fictional part of the array.

You can copy and paste out of the command prompt, I believe. I forget how, but just fiddle around with it until you can post the exact error.

Edit: I can't see anything wrong with the way you're doing things. The only issue I see is that you never assigned a value to count and you're trying to use it in your for loops in your methods. That's the only [possible] issue I see.

This post has been edited by Locke: 08 July 2009 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 smith123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-June 09

Re: error out of bound exception

Posted 08 July 2009 - 09:45 AM

Java.langarray out of bound exception 3
cant copy and pase
line 168

line 168 is

MovieData [ j ] [ k ] = object.nextToken();

in code
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: error out of bound exception

Posted 08 July 2009 - 10:15 AM

Oh, well that makes things more interesting. You have no set limit on what k can go up to in that while loop. It went up to 3 and then the array was accessed at that index, throwing the exception.

You need to reset k INSIDE the for loop. You have k = 0; as the statement before it. So right now, once k hits 3, it's never reset, even if a new record is moved on to (j is increased). Move k = 0; to the first statement inside the loop. That should fix your problem.

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#5 smith123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-June 09

Re: error out of bound exception

Posted 08 July 2009 - 10:24 AM

That just prints a bunch of nulls
Was This Post Helpful? 0
  • +
  • -

#6 smith123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-June 09

Re: error out of bound exception

Posted 08 July 2009 - 11:50 AM


MovieData = new String [ Records.length ][3];
						
						
for ( j = 0; j < Records.length; j++ ) {
	Records[j] = Records[j].trim()						}				
	for ( j = 0; j < Records.length; j++ ) {
						
	k = 0;
						StringTokenizer object = new StringTokenizer(Records[j],"~");
					
						
							while(object.hasMoreTokens()  && k < 3)  { // begin while
																		MovieData [ j ] [ k ] = object.nextToken();
								k++; 
								}// end while
								count++;						
		} // end for statement	

	for ( j = 0; j < Records.length; j++ ) {
							for ( k = 0; k < 3; k++ ) {
								System.out.print(MovieData[j][k]);
							} // end inner for statement
	}// end outer for statement			
		



This just prints null. I even trimmed the Record array. I don't get it. Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: error out of bound exception

Posted 08 July 2009 - 12:57 PM

I'll take a closer look at it, but something I see right off the bat is the fact that you're using two for loops. Use one. :)

for (j = 0; j < Records.length; j++)
{
    Records[j] = Records[j].trim();

    k = 0;

    // ...

    while (/* ... */)
    {
        // ...
    }

    // ...
}


The only other thing that might cause an issue is that if the ~ is not the delimiter. Then each record would have 1 token and the other 2 pieces of the second dimension of the array would be null.

This post has been edited by Locke: 08 July 2009 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1