Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sharing an array between classes

Status
Not open for further replies.

nick2price2003

Technical User
Feb 14, 2008
1
GB
I have created a database class which creates a database object array with three objects in each index.
Code:
class Database
{ 
    
	int answer = JOptionPane.YES_OPTION; 
	int count = 0;
	final int ARRAY_SIZE = 12;
	
public Database(){
	
	
	CD[] data = new CD[ARRAY_SIZE];
	String[] search = new String[ARRAY_SIZE];
	
	while (answer == JOptionPane.YES_OPTION)
	{

	String a;
	String c;
	int n;
	
	a = JOptionPane.showInputDialog("Please, enter artist name");
	c = JOptionPane.showInputDialog("Please, enter CD name");
	n = Integer.parseInt(JOptionPane.showInputDialog("Please enter total number of tracks"));

	data[count] = new CD(a, c, n);
	search[count]= new String(c);

    answer = JOptionPane.showConfirmDialog(null, "Enter another record?",
	   		   "???", JOptionPane.YES_NO_OPTION);
	count++;
	}
 }
}
I then created a new class called search that is supposed to search the array in my database class for whatever the user types in, which in this case is a cd name.
Code:
class Search
{
        int result; 
	    int ARRAY_SIZE=12;
		String searchKey = JOptionPane.showInputDialog("Give me the name of a CD");
		
		result = linearSearch(search, searchKey, ARRAY_SIZE);
	
		if(result== -1)
		{
		JOptionPane.showMessageDialog(null,"KEY " + searchKey + " NOT FOUND");
		}
		else 
		{ 
		JOptionPane.showMessageDialog(null,"KEY " + searchKey + " FOUND in position " + result);
		}

   
   
public static int linearSearch(String[] search, String key, int sizeOfArray)
	{
		for (int counter = 0; counter < sizeOfArray; counter++)
		{
		if (search[counter].equalsIgnoreCase (key))
		return counter; 
		}
		return -1;
	}
 }
How do i get the array from the database class to the search class.

cheers
 
Code a getArray method on the database class that returns the array.

And maybe you should take a look at one OOP tutorial.

Cheers,
Dian
 
I think that in your case it would be better to create a single input dialog where the user can enter the artist name, cd name and number of tracks in one go. You can add there validation routines to see whether the user of the program enter the correct data. The way it is right now the user will see too many dialogs popping up where he has to enter data. Apart from having to click too many times on an OK button your program will abort if the user doesn't enter a numerical value in the "Please enter total number of tracks" inputdialog.

You might also want to reconsider whether to use a Collection instead of an array, because if the user of your programs enters more than 12 records you program will abort with an IndexOutOfBoundsException. This doesn't happen when you use f.e a Vector or an ArrayList because they will grow dynamically. Another advantage of Collections is that you can more easily check whether it contains a value or not using the isEmpty(), indexOf(Object) or contains(Object) methods.

Good Luck,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top