nick2price2003
Technical User
I have created a database class which creates a database object array with three objects in each index.
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.
How do i get the array from the database class to the search class.
cheers
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++;
}
}
}
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;
}
}
cheers