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)
-
Assignment_3.zip (6.44K)
Number of downloads: 40

New Topic/Question
Reply




MultiQuote




|