This is my first time writing an array of objects and I’m having an issue putting it all together.

I have written two classes and the third program that is supposed to test the classes.

***PROBLEM***
My primary issue at the moment is that my program to test the classes has to read data from an external file and place it into an array.

The program to test my classes creates an instance of an array of objects that is initially empty.

Next it needs to pull what data is in the external file and place it in the array (for loop)
I’m not sure how to loop each line form the file into the array.

My second problem is going to be once i have the external file into my array how do i pull each array value out of the index and write it back to the same file

again i know it needs to be a loop but I'm not sure how to write a for statement like this

for(int index = 0; index < DVDcollection(indexvalue of array); index++)

because the size of the array is stored in the class how do i say for given array index?


Below is a description of what my array of objects will require followed by what code I have so far.


Program 7: Creating an Array of Objects - Part I
Using the Library Books program as a guide, write your own program that allows you to create a database of your own.
Include the following in your program:
1. Create a Class representing the information in one item of your database. The information for one item must include a minimum of 3 fields. Within the Class for one item, include the following methods:
a. A constructor that accepts the information about one item and assigns it to the instance data of the method.
b. A method that prints the information about one item of your collection. Name this method toString. NOTE: It is important that you name this method toString because it's one of Java's built-in methods that allows you print out your Object.


*******MY CODE FOR THIS SECTION*********

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVD
*
* Class constructor will initialize the following:
*    Instance Data
*		1. Title
*		2. Genre
*		3. Rateing
*		4. Description
* Class will include a separate method to perform each of the following:
*  1.	toString() will return instance data for object.
*
*Initial Algorithm:
*
* BEGIN Program 7
*
* Public DVD()
* Public toString()
*
* END Program 7
 *********************************************************************/

 //Pre-Processor Declaration Section
 public class DVD
 {

	private String Title;
	private String Genre;
	private double Rateing;
	private String Description;
	String Result;

	public DVD(String dvdname, String type, double quality, String info) //******CONSTRUCTOR TO INITIALIZE TITLE, GENRE, RATEING, AND DESCRIPTION******//
	{// START CONSTRUCTOR
		Title= dvdname;
		Genre= type;
		Rateing=quality;
		Description=info;


	} // END CONSTRUCTOR



	public String toString() //***** METHOD PROVIDES A STRING REPRESENTATION OF CLASS OBJECT******//
	{//START toSTRING

		String Result="Title :\t"+Title+ "\nGenre :\t"+Genre+"\nRateing :\t"+Rateing+"\nDescription:\n\t"+Description;

		return Result;

	}//END toSTRING

}//END DVD CLASS

2. Create another Class with an array of Objects of the Class of one item with the following methods:
a. Create an array of the Objects from the Class of one item.
b. Create a constructor to initialize the number of items in the array to zero.
c. Create a method to add one item at a time to the array.
d. Create a method to display the contents of the entire array. Call this method toString.
e. Create a method to increase the size of the array if it is getting full.


*******MY CODE FOR THIS SECTION*********

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
*	Initial Algorithm:
*
*	public Class DVDBOOK-Array of objects.
*   {
*	Instance data:
*
*
*   DVDBOOK() - constructor
*	{
*	Initializes number of items in the array to zero.
*	}
*
*	ADDDVD()
*	{
*	Method Adds one Item to the collection.
*	}
*
*	toString()
*	{
*	Method displays contents of the entire array.
*	}
*
*	arrayPlus()
*	{
*	Mehtod to increase the size of the array if ti is gettign small.
*	}
* } end DVDBOOK
*
 *********************************************************************/

 //Pre-Processor Declaration Section

 	public class DVDBOOK // START DVDBOOK CLASS
 	{

	 //***INSTANCE DATA***//
     final int MAX_DVDS= 100; // SIZE OF DVDBOOK ARRAY

	 private DVD[] BOOK = new DVD[MAX_DVDS]; //DELCARATION OF ARRAY DVD

	 private int count;

	 //******CONSTRUCTOR: CREATES AN INITIALLY EMEPTY ARRAY******//
	 public DVDBOOK()
	 {

		 count=0;
	 }


	 //******ADDDVD() METHOD ADDS ONE DVD ITEM TO THE COLLECTION****//
	 public void ADDDVD(String Title, String Genre, double Rateing, String Description)
	 {
		 if (count == BOOK.length)
		 arrayPlus();

		 BOOK[count] = new DVD(Title, Genre, Rateing, Description);

		 count++;
	 }

	 //******toSTRING() METHOD TO DISPLAY CONTENTS OF ENTIRE ARRAY******//
	 public String toString()
	 {
		String report="MY DVDBOOK \n\nNumber of DVD's: "+count+"\n\n";

		for(int dvd  = 0; dvd < count; dvd ++)
		report+=BOOK[dvd]+"\n";

		return report;
	}

	//******arrayPLUS()METHOD INCREASES ARRAY SIZE IF IT IS FULL******//
	public void arrayPlus()
	{
		DVD[] temp = new DVD[BOOK.length*2];

		for(int dvd = 0; dvd < BOOK.length; dvd ++)
		temp[dvd] = BOOK[dvd];

		BOOK = temp;
	}

}//END DVDBOOOK CLASS







	;

3. Create a Driver Class with a Menu with the option to add an item, to print out a list of all items, and exit.
4. When your program is first executed, read the data from an external file and place it in your array. Submit the external file with program.
5. Save the data back to the external file when the Exit option is chosen from the menu in the Drive.
NOTE: You MUST read your data from an external file! You MUST save your data back to that external file when done.

*******MY CODE FOR THIS SECTION*********

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
*	Initial Algorithm:
*
*	public Class DVDCOLLECTION
*   {
*	Instance data:
*	CREATE INSTANCE OF DVDBOOK
*
*	OPEN FILE:
*
*	FILE READER
*	BUFFERED READER
*	BUFFERED WRITER
*
*	PLACE FILE TOKENS INTO INSTANCE OF DVDBOOK
*
*
*	do
*	{
*		DISPLAY MENUE WITH OPTIONS
*		1. ADD A DVD
*		2. DISPLAY COLLECTION
*		3. EXIT
*
*		READ INPUT FROM USER
*
*	}
*	IF (INPUT1)
*	{
*		ADD A OBJ.DVD() TO DVDBOOK[]
*	}
*	IF (INPUT2)
*	{
*		TOSTRING()
*	}
*	IF (INPUT3)
*	{
*		EXIT
*	}
*	WHILE(INPUT!=3)
*
*	WRITE DVDBOOK TO FILE
*
*	CLOSE FILE
*
*	} END DVDCOLLECTION
*
*
*
*
 *********************************************************************/

 //Pre-Processor Declaration Section
 import java.io.*;
 import java.util.*;

 	public class DVDCOLLECTION
 	{

		public static void main (String args[])
		{// BEGIN MAIN METHOD



			//*****INSTANCE DATA******//


			 DVDBOOK DVDcollection = new DVDBOOK(); //***INSTANCE OF DVDBOOK CLASS***//
			 int menuItem;
			 String Title,Genre,Description;
			 double Rateing;
			 char sure;




			 //**********************READS EXTERNAL FILE INTO ARRAY*******************************//


			 						//*******PROBLEM STARTS HERE*******//


				for (int index = 0; index < DVDBOOK.length; index++)//
				{
				 try
				 {//***START TRY***//
				 String inPutFile="TESTDATA";
				 FileReader fRead = new FileReader(inPutFile);
				 BufferedReader bRead = new BufferedReader(fRead);


				 String Line = bRead.readLine();
				 StringTokenizer Tokenizer;

				 while(Line !=null)
				{
					 Tokenizer = new StringTokenizer(Line);

				     DVDcollection[index] = Tokenizer.nextToken()+Tokenizer.nextToken()+Tokenizer.nextToken()+Tokenizer.nextToken();

				     Line = bRead.readLine();
				}

				 bRead.close(); //***CLOSE FILE***//

			  }//***END TRY***///

			  catch (IOException exception)
			   {// START CATCH

			  		System.out.println(exception.getMessage());

			   }//END CATCH






			//************************************************************************************//


			//*******************************DISPLAY MENU*****************************************//

			do
			{
				System.out.println ("\n\n\nSelect from the following:  \n");
				System.out.println ("1.  Add a DVD\n");
				System.out.println ("2.  Display List of DVD's\n");
				System.out.println ("3.  Exit\n\n");

   				menuItem = Keyboard.readInt( );


   				if (menuItem == 1)//******USER CHOOSES TO ADD A DVD******//
   				{
						//******USER INPUT DVD INFO******//

				 		System.out.println("Enter the title of the DVD: ");
						Title=Keyboard.readString();

						System.out.println("Enter the genre of the DVD: ");
						Genre=Keyboard.readString();

						System.out.println("Enter the rateing for the DVD 1 to 10");
						Rateing=Keyboard.readDouble();

						System.out.println("Enter a brief descriptoin of the DVD: ");
						Description=Keyboard.readString();

						//******NEW DVD ADDED TO DVDBOOK() DVDcollection******//
						DVDcollection.ADDDVD(Title, Genre, Rateing, Description);
				}

				else if (menuItem == 2)
				{
						System.out.println (DVDcollection.toString());  //**PRINTS toSting()*****//
				}

				else
			   {

				    	System.out.println ("Are you sure you want to quit? Y/N:  \n");
				        sure = Keyboard.readChar( );

				        if (sure == 'n' || sure == 'N')
				        menuItem = 1;
				        else
				        menuItem = 3;
				 }

				 }// end while
				  while (menuItem != 3);  //while exit hasn't been selected



				 }//end main

}//end class

}

Sense I posted my initial problem I have re-worked some of my DVDBOOK class and the DVDCOLLECTION to test my classes

the problem I am still having is that I am trying to find the length of my array to assign to int count so that my loop will run.

but i don't know how I am supposed to find the length of the array outside the class that creates it.

NEW DVDBOOK CLASS

**************************************************************************************

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
*	Initial Algorithm:
*
*	public Class DVDBOOK-Array of objects.
*   {
*	Instance data:
*
*
*   DVDBOOK() - constructor
*	{
*	Initializes number of items in the array to zero.
*	}
*
*	ADDDVD()
*	{
*	Method Adds one Item to the collection.
*	}
*
*	toString()
*	{
*	Method displays contents of the entire array.
*	}
*
*	arrayPlus()
*	{
*	Mehtod to increase the size of the array if ti is gettign small.
*	}
* } end DVDBOOK
*
 *********************************************************************/

 //Pre-Processor Declaration Section

 	public class DVDBOOK // START DVDBOOK CLASS
 	{

	 //***INSTANCE DATA***//
     final int MAX_DVDS= 100; // SIZE OF DVDBOOK ARRAY

	 DVD[] DVDBOOK = new DVD[MAX_DVDS]; //DELCARATION OF ARRAY DVD

	 int count;

	 //******CONSTRUCTOR: CREATES AN INITIALLY EMEPTY ARRAY******//
	 public DVDBOOK ()
	 {

		 count=0;
	 }


	 //******ADDDVD() METHOD ADDS ONE DVD ITEM TO THE COLLECTION****//
	 public void ADDDVD(String Title, String Genre, double Rateing, String Description)
	 {
		 if (count == DVDBOOK.length)
		 arrayPlus();

		 DVDBOOK[count] = new DVD(Title, Genre, Rateing, Description);

		 count++;
	 }

	 //******toSTRING() METHOD TO DISPLAY CONTENTS OF ENTIRE ARRAY******//
	 public String toString()
	 {
		String report="MY DVDBOOK \n\nNumber of DVD's: "+count+"\n\n";

		for(int index  = 0; index < count; index ++)
		report+=DVDBOOK[index]+"\n";

		return report;
	}

	//******arrayPLUS()METHOD INCREASES ARRAY SIZE IF IT IS FULL******//
	public void arrayPlus()
	{
		DVD[] temp = new DVD[DVDBOOK.length*2];

		for(int index = 0; index < DVDBOOK.length; index ++)
		temp[index] = DVDBOOK[index];

		DVDBOOK = temp;
	}

}//END DVDBOOOK CLASS







	;

NEW DVDCOLLECTION CLASS

**************************************************************************************

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
*	Initial Algorithm:
*
*	public Class DVDCOLLECTION
*   {
*	Instance data:
*	CREATE INSTANCE OF DVDBOOK
*
*	OPEN FILE:
*
*	FILE READER
*	BUFFERED READER
*	BUFFERED WRITER
*
*	PLACE FILE TOKENS INTO INSTANCE OF DVDBOOK
*
*
*	do
*	{
*		DISPLAY MENUE WITH OPTIONS
*		1. ADD A DVD
*		2. DISPLAY COLLECTION
*		3. EXIT
*
*		READ INPUT FROM USER
*
*	}
*	IF (INPUT1)
*	{
*		ADD A OBJ.DVD() TO DVDBOOK[]
*	}
*	IF (INPUT2)
*	{
*		TOSTRING()
*	}
*	IF (INPUT3)
*	{
*		EXIT
*	}
*	WHILE(INPUT!=3)
*
*	WRITE DVDBOOK TO FILE
*
*	CLOSE FILE
*
*	} END DVDCOLLECTION
*
*
*
*
 *********************************************************************/

 //Pre-Processor Declaration Section
 import java.io.*;
 import java.util.*;

 	public class DVDCOLLECTION2
 	{

		public static void main (String args[]) throws IOException
		{// BEGIN MAIN METHOD

			/*********************************************************************************
											INSTANCE DATA
			*********************************************************************************/

			DVDBOOK DVDcollection = new DVDBOOK(); //INSTANCE OF DVD BOOK CLASS NAMED DVDcollection.

			int menuItem;  // USERS MENU SELECTION.

			char sure; //USERS CHANCE TO NOT EXIT.

			String Title,Genre,Description; // STRING VARIABLES FOR DVD()

			double Rateing; //DOUBLE VARIABLES FOR DVD()

			//*****int count =  ; //     <= *****NEED PROPER METHOD TO DEFINE ARRAY LENGTH************

			String outPutFile = "TESTDATA";

			/*********************************************************************************
									READS EXTERNAL FILE INTO ARRAY
			*********************************************************************************/

			Scanner EXDAT = new Scanner (new File("TESTDATA"));

			while (EXDAT.hasNextLine())
			{//START WHILE

				String Line = EXDAT.nextLine();


				for(int index=0; index < count; index++)
				{//START FOR


				DVDcollection[index] = Line;

				Line = EXDAT.nextLine();

				}//END FOR

			}// END WHILE

			EXDAT.close();






			/*********************************************************************************
											DISPLAY MENU
			*********************************************************************************/



						do
						{//START DO-WHILE

							System.out.println ("\n\n\nSelect from the following:  \n");
							System.out.println ("1.  Add a DVD\n");
							System.out.println ("2.  Display List of DVD's\n");
							System.out.println ("3.  Exit\n\n");

			   				menuItem = Keyboard.readInt( );


			   				if (menuItem == 1)//******USER CHOOSES TO ADD A DVD******//
			   				{//START IF

									//******USER INPUT DVD INFO******//

							 		System.out.println("Enter the title of the DVD: ");
									Title=Keyboard.readString();

									System.out.println("Enter the genre of the DVD: ");
									Genre=Keyboard.readString();

									System.out.println("Enter the rateing for the DVD 1 to 10");
									Rateing=Keyboard.readDouble();

									System.out.println("Enter a brief descriptoin of the DVD: ");
									Description=Keyboard.readString();


									//******NEW DVD ADDED TO DVDBOOK() DVDcollection******//
									DVDcollection.ADDDVD(Title, Genre, Rateing, Description);


							}//END IF


							else if (menuItem == 2)
							{//START ELSE-IF

									System.out.println (DVDcollection.toString());  //**PRINTS toSting()*****//

							}//END ELSE-IF

							else
						    {//START ELSE

							    	System.out.println ("Are you sure you want to quit? Y/N:  \n");
							        sure = Keyboard.readChar( );

							        if (sure == 'n' || sure == 'N')
							        menuItem = 1;
							        else
							        menuItem = 3;

							}//END ELSE

							}// END DO WHILE
							while (menuItem != 3);  //WHILE EXIT HAS NOT BEEN SELECTED



		 /*********************************************************************************
							WRITES DVDcollection TO EXTERANL FILE NAMED TESTDATA
		  *********************************************************************************/

							FileWriter fileWrite = new FileWriter(outPutFile);
							BufferedWriter buffWrite = new BufferedWriter(fileWrite);
							PrintWriter outFile = new PrintWriter(fileWrite);

						for(int index=0; index < count; index++)
						{//START FOR

						outFile.println(DVDcollection[index]);

						}//END FOR

						buffWrite.flush();
						outFile.close();

					}//END MAIN METHOD

				}//END DVDCOLLECTION

Below are the errors I get when I attempt to compile the test program.

C:\Users\peter\Desktop\CSC 111\welch_program7\DVDCOLLECTION2.java:101: cannot find symbol
symbol : variable count
location: class DVDCOLLECTION2
for(int index=0; index < count; index++)
^
C:\Users\peter\Desktop\CSC 111\welch_program7\DVDCOLLECTION2.java:105: array required, but DVDBOOK found
DVDcollection[index] = Line;
^
C:\Users\peter\Desktop\CSC 111\welch_program7\DVDCOLLECTION2.java:195: cannot find symbol
symbol : variable count
location: class DVDCOLLECTION2
for(int index=0; index < count; index++)
^
C:\Users\peter\Desktop\CSC 111\welch_program7\DVDCOLLECTION2.java:198: array required, but DVDBOOK found
outFile.println(DVDcollection[index]);
^
4 errors

Tool completed with exit code 1

Read the error messages that you get. They tell you exactly what the problem is:
cannot find symbol
symbol : variable count

The program cannot find the count variable. Have you defined it? Is it visible?

Also when you do: DVDcollection[index] , the DVDcollection needs to be an array. The error messages tells you exactly that.

The messages also tell you the lines where the error occurred.

I have made a lot of changes fixing all of my initial problems with the program it will compile now and it runs. the only issue I am having now is that when my driver reads the TESTDATA.txt file it keeps throwing the exception that the value is null but the data is in the file but my program still writes the dvd's to the file correctly I just cant pull the data back out when I run it a second time. Any suggestions would be greatly appreciated.


Below are my new working classes

DVD CLASS:UNIT OF ONE

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVD
*
* Class constructor will initialize the following:
*    Instance Data
*		1. Title
*		2. Genre
*		3. Rating
*		4. Description
* Class will include a separate method to perform each of the following:
*  1.	toString() will return instance data for object.
*
*Initial Algorithm:
*
* BEGIN Program 7
*
* Public DVD()
* Public toString()
*
* END Program 7
 *********************************************************************/

 //Pre-Processor Declaration Section
 public class DVD
 {

	private String Title;
	private String Genre;
	private String Rating;
	private String Description;
	String Result;

	public DVD(String dvdname, String type, String quality, String info) //******CONSTRUCTOR TO INITIALIZE TITLE, GENRE, RATEING, AND DESCRIPTION******//
	{// START CONSTRUCTOR
		Title= dvdname;
		Genre= type;
		Rating=quality;
		Description=info;


	} // END CONSTRUCTOR



	public String toString() //***** METHOD PROVIDES A STRING REPRESENTATION OF CLASS OBJECT******//
	{//START toSTRING

		String Result="Title :\t"+Title+ "\nGenre :\t"+Genre+"\nRating :\t"+Rating+"\nDescription:\n\t"+Description;

		return Result;

	}//END toSTRING

}//END DVD CLASS

DVDBOOK CLASS: ARRAY OF OBJECTS

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
*	Initial Algorithm:
*
*	public Class DVDBOOK-Array of objects.
*   {
*	Instance data:
*
*
*   DVDBOOK() - constructor
*	{
*	Initializes number of items in the array to zero.
*	}
*
*	ADDDVD()
*	{
*	Method Adds one Item to the collection.
*	}
*
*	toString()
*	{
*	Method displays contents of the entire array.
*	}
*
*	arrayPlus()
*	{
*	Method to increase the size of the array if it is getting small.
*	}
* } end DVDBOOK
*
 *********************************************************************/

 //Pre-Processor Declaration Section

 	public class DVDBOOK // START DVDBOOK CLASS
 	{

	 //***INSTANCE DATA***//
     final int MAX_DVDS= 100; // SIZE OF DVDBOOK ARRAY

	 DVD[] DVDBOOK = new DVD[MAX_DVDS]; //DELCARATION OF ARRAY DVD

	 int count;

	 //******CONSTRUCTOR: CREATES AN INITIALLY EMEPTY ARRAY******//
	 public DVDBOOK ()
	 {

		 count=0;
	 }


	 //******ADDDVD() METHOD ADDS ONE DVD ITEM TO THE COLLECTION****//
	 public void ADDDVD(String Title, String Genre, String Rateing, String Description)
	 {
		 if (count == DVDBOOK.length)
		 arrayPlus();

		 DVDBOOK[count] = new DVD(Title, Genre, Rateing, Description);

		 count++;
	 }

	 //******toSTRING() METHOD TO DISPLAY CONTENTS OF ENTIRE ARRAY******//
	 public String toString()
	 {
		String report="MY DVDBOOK \n\nNumber of DVD's: "+count+"\n\n";

		for(int index  = 0; index < count; index ++)
		report+=DVDBOOK[index].toString()+"\n";

		return report;
	}

	//******arrayPLUS()METHOD INCREASES ARRAY SIZE IF IT IS FULL******//
	public void arrayPlus()
	{
		DVD[] temp = new DVD[DVDBOOK.length*2];

		for(int index = 0; index < DVDBOOK.length; index ++)
		temp[index] = DVDBOOK[index];

		DVDBOOK = temp;
	}

	public int length()
	{
		return DVDBOOK.length;
	}


}//END DVDBOOOK CLASS

DVDCOLLECTION DRIVER : TESTS ARRAY OF OBJECTS

//Prologue Section
/**********************************************************************
*Program Name:   CSC 111 Program 7
*Author:    Peter Welch
*Date:     4/24/11
*Course/Section:  CSC 111-001 (002w)
*Program Description:
*A java class that will manage a DVDBOOK
*
*	Initial Algorithm:
*
*	public Class DVDCOLLECTION
*   {
*	Instance data:
*	CREATE INSTANCE OF DVDBOOK
*
*	OPEN FILE:
*
*	FILE READER
*	BUFFERED READER
*	BUFFERED WRITER
*
*	PLACE FILE TOKENS INTO INSTANCE OF DVDBOOK
*
*
*	do
*	{
*		DISPLAY MENUE WITH OPTIONS
*		1. ADD A DVD
*		2. DISPLAY COLLECTION
*		3. EXIT
*
*		READ INPUT FROM USER
*
*	}
*	IF (INPUT1)
*	{
*		ADD A OBJ. DVD() TO DVDBOOK[]
*	}
*	IF (INPUT2)
*	{
*		TOSTRING()
*	}
*	IF (INPUT3)
*	{
*		EXIT
*	}
*	WHILE (INPUT!=3)
*
*	WRITE DVDBOOK TO FILE
*
*	CLOSE FILE
*
*	} END DVDCOLLECTION
*
*
*
*
 *********************************************************************/

 //Pre-Processor Declaration Section
 import java.io.*;
 import java.util.*;

 	public class DVDCOLLECTION
 	{

		public static void main (String args[]) throws IOException
		{// BEGIN MAIN METHOD

			/*********************************************************************************
											INSTANCE DATA
			*********************************************************************************/

			DVDBOOK DVDcollection = new DVDBOOK(); //INSTANCE OF DVD BOOK CLASS NAMED DVDcollection.

			int menuItem;  // USERS MENU SELECTION.

			char sure; //USERS CHANCE TO NOT EXIT.

			String Title,Genre,Description; // STRING VARIABLES FOR DVD()

			String Rating; //DOUBLE VARIABLES FOR DVD()

			int count =  DVDcollection.length();

			String outPutFile = "TESTDATA.txt";


			/*********************************************************************************
									READS EXTERNAL FILE INTO ARRAY
			*********************************************************************************/

			try
			{
				FileReader fRead = new FileReader(outPutFile);
				BufferedReader bRead = new BufferedReader(fRead);

				String line = bRead.readLine();

				for (int index = 0; index < count; count++)
				{
				   line = bRead.readLine();

					StringTokenizer tokenizer = new StringTokenizer(line);

					while(tokenizer.hasMoreTokens())
					{


					Title=tokenizer.nextToken();
					Genre=tokenizer.nextToken();
					Rating=tokenizer.nextToken();
					Description=tokenizer.nextToken();
					DVDcollection.ADDDVD(Title, Genre, Rating, Description);
					}

					line = bRead.readLine();
				}
				bRead.close();
			}

			catch (IOException exception)
				{
					System.out.println(exception.getMessage());
				}

			catch (NoSuchElementException exception)
			{
				System.out.println(exception.getMessage());
			}


			/*********************************************************************************
											DISPLAY MENU
			*********************************************************************************/



						do
						{//START DO-WHILE

							System.out.println ("\n\n\nSelect from the following:  \n");
							System.out.println ("1.  Add a DVD\n");
							System.out.println ("2.  Display List of DVD's\n");
							System.out.println ("3.  Exit\n\n");

			   				menuItem = Keyboard.readInt( );


			   				if (menuItem == 1)//******USER CHOOSES TO ADD A DVD******//
			   				{//START IF

									//******USER INPUT DVD INFO******//

							 		System.out.println("Enter the title of the DVD: ");
									Title=Keyboard.readString();

									System.out.println("Enter the genre of the DVD: ");
									Genre=Keyboard.readString();

									System.out.println("Enter the rating for the DVD 1 to 10");
									Rating=Keyboard.readString();

									System.out.println("Enter a brief description of the DVD: ");
									Description=Keyboard.readString();


									//******NEW DVD ADDED TO DVDBOOK() DVDcollection******//
									DVDcollection.ADDDVD(Title, Genre, Rating, Description);


							}//END IF


							else if (menuItem == 2)
							{//START ELSE-IF

									System.out.println (DVDcollection.toString());  //**PRINTS toSting()*****//

							}//END ELSE-IF

							else
						    {//START ELSE

							    	System.out.println ("Are you sure you want to quit? Y/N:  \n");
							        sure = Keyboard.readChar( );

							        if (sure == 'n' || sure == 'N')
							        menuItem = 1;
							        else
							        menuItem = 3;

							}//END ELSE

							}// END DO WHILE
							while (menuItem != 3);  //WHILE EXIT HAS NOT BEEN SELECTED

		 /*********************************************************************************
							WRITES DVDcollection TO EXTERANL FILE NAMED TESTDATA
		  *********************************************************************************/

					try
					{//START TRY
							FileWriter fileWrite = new FileWriter(outPutFile);
							BufferedWriter buffWrite = new BufferedWriter(fileWrite);
							PrintWriter outFile = new PrintWriter(fileWrite);

							outFile.println(DVDcollection);


						/*for(int index=0; index < count; index++)
						{//START FOR

						outFile.println(DVDcollection);

						}//END FOR*/
						buffWrite.flush();
						buffWrite.close();

					}//END TRY



					catch (IOException exception)
						{//START CATCH

							System.out.println(exception.getMessage());

						}//END CATCH

					}//END MAIN METHOD

				}//END DVDCOLLECTION

The problem is with this code. The idea is that you should read one line, then parse it, then read the next. But what you do instead, is read the first line, then do nothing, then enter the loop read again the next line and after you parse it, you read again the next line, with out doing anything, AND then you go at the beginning of the loop.

In total you read 3 lines!, but only parsed one, and the loop (index<count) run only once. If your file has 'count' lines, but inside the loop you read 2 lines each time, then the file will run out of lines:

File:
>1
>2
>3

You loop 3 times, but at the first loop you have already read the first 3 lines, so the next iteration will have no lines to read:

String line = bRead.readLine(); // READ THE FIRST LINE

for (int index = 0; index < count; count++)
{
line = bRead.readLine(); // THEN READ THE NEXT LINE
StringTokenizer tokenizer = new StringTokenizer(line);

while(tokenizer.hasMoreTokens())
{
Title=tokenizer.nextToken();
Genre=tokenizer.nextToken();
Rating=tokenizer.nextToken();
Description=tokenizer.nextToken();
DVDcollection.ADDDVD(Title, Genre, Rating, Description); // ONE LINE INSERTED
}

line = bRead.readLine(); // AND AGAIN READ THE NEXT LINE
}

You read 2 lines in one iteration of the loop. Since the loop runs count times, you will read 2*count lines, but the file has only count lines.

The idea, is in the loop:

for loop {
  read line
  
  parse line
}

You don't need to initialize 'line' outside the loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.