Hello,

Working on my final project for a Java Class and have become stumped on the final leg of the code.

We are supposed to create a program to add a record, remove a record, display a record, and list all records. The records must be written to a file.

I have all of it working except the file part. I am able to get the program to write my array, (Which is what I am using to store and manipulate the records during program running), but I am having trouble getting it to read in the contents Back into the array.

The relevant code is:

/*--------------------SAVE DATABASE-----------------------*/
	public static void SaveDatabase(String plateDB[][])
	{
	String tempFileName = "";
	Scanner inputDevice = new Scanner(System.in);
	
	System.out.print("Please enter a filename to save database as:");
	tempFileName = inputDevice.nextLine();
	
	File database = new File(tempFileName);
	
	try
	{
	 FileOutputStream fos = new FileOutputStream(database);
  	 ObjectOutputStream oos = new ObjectOutputStream(fos);
   
  	 oos.writeObject(plateDB);  // Write the String array to a file
 	 oos.close();
	
	}
	catch(IOException e)
	{
		System.err.println("Error opening file!");
	}
				
	}

This is what I am using to write the array out to the file, It asks for a file name and just saves to the file, (I intend to add some error handling later, like if the file already exists).

/*--------------------LOAD DATABASE-----------------------*/	
	public static String[][] LoadDatabase()
	{
   String[][] plateDB = new String[100][3];
	String tempFileName = "";
	Scanner inputDevice = new Scanner(System.in);
	
	System.out.print("Please enter a filename to open:");
	tempFileName = inputDevice.nextLine();
	
	try
	{
	FileInputStream fis = new FileInputStream(new File (tempFileName));
   ObjectInputStream ois = new ObjectInputStream(fis);
   plateDB = (String[][])ois.readObject();
	ois.close();
	}
	catch(IOException e)
	{
		System.err.println("Error opening file!");
	}
	catch(ClassNotFoundException e)
	{
		System.err.println("Class Not Found!");
	}

	return plateDB;

	}

This is what I have to read in the code from the file, (Again, error handling later, like if the file does not exist).

I have tested the save code, and it does create a file with the data in it. I just can not get it to read in the code, or if it does, it does not pass the array back to the calling area. Thank you for any pointers!

I cant see why the reading part wont work. What youve done is correct. If you could post the method that calls this method, may be we cant see this more clearly.

And BTW, try printing the data you've received in this method (LoadDatabase) itself to make sure the reading isn't wrong.

Good idea printing out the data to see if it has it. Here is the main class, it calls the methods from another class as required by the assignment.

import java.io.*;
import java.util.*;

public class Dmv
{

	public static void main(String[] args)
	{
		//Declare Variables:
		int menuChoice;
		String[][] plateDB = new String[100][3];
		
		Scanner inputDevice = new Scanner(System.in);
		
		//Display and Choose from Menu
		do
		{
			DMV_Class.Menu();
			menuChoice = inputDevice.nextInt();
			while(menuChoice < 0 || menuChoice > 6)
			{
				System.out.println("---INCORRECT MENU OPTION---");
				DMV_Class.Menu();
				menuChoice = inputDevice.nextInt();
				inputDevice.nextLine();
			}
			switch(menuChoice)
			{
				case 0:
				{
					DMV_Class.AddAPlate(plateDB);
					break;
				}
				case 1:
				{
					DMV_Class.RemoveAPlate(plateDB);
					break;
				}
				case 2:
				{
					DMV_Class.ListPlates(plateDB);
					break;
				}
				case 3:
				{
					DMV_Class.DisplayAPlate(plateDB);
					break;
				}
				case 4:
				{
					DMV_Class.LoadDatabase();
					break;
				}
				case 5:
				{
					DMV_Class.SaveDatabase(plateDB);
					break;
				}
				case 6:
				{
					DMV_Class.EndRun();
					break;
				}
			}
		}while(menuChoice != 6);

		
	}
}

You are correct, I inserted a call to the listplates method, passing the array to it and it does display the plates from the file on a load operation, however, the calling class does not get this information.

Is there a pointer you could give me as to why it is not returning the data? I do have a return statement in the load method.

Dude, you're calling the method. OK!
You're returning String[][] OK!
But where are you storing the returned object??

You must have something like

plateDB =DMV_Class.LoadDatabase();

Doh! Thank you so much! I apparently need some more Red Bull!

Hehehe! Python gives you wings as well!
Anyway mark the thread as solved...!

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.