I am a newbie in Java. I am trying to create a Class named Database that will read a text file to create an array. There is no main method in this code since I have another java file that acts as a main application, and it has the main method.
Here is my code:
public class Database {
public static String[][] items = new String[20][3];
public static String[][] fillArray(String myFile)
{
TextFileInput in = new TextFileInput(myFile);
for(int i=0; i<20; i++)
{
String line =in.readLine();
StringTokenizer token = new StringTokenizer(line, ",");
for(int j=0; j<3; j++)
{
items[i][j] = token.nextToken();
}
}
in.close();
return items;
}
public static String getName(String code)
{
for(int i=0; i<20; i++)
{
if (code.equals(items[i][0]))
return items[i][1];
}
}
public static String getPrice(String code)
{
for(int i=0; i<20; i++)
{
if(code.equals(items[i][0]))
return items[i][2];
}
}
}
Question 1: Eclipse shows errors by indicating on both methods (getName and getPrice). It says: " This method must return a result of type String". Can anyone please explain and fix this?
Question 2: This Database class includes methods and an array created by reading in the text files. And I have another java file which is the main application file that includes the main method. The main application file is the file where I would like to create object and call the methods in. I understand the concept, but I keep getting errors when I try to create a database object and call the methods on this object. Can anyone please show me an example by using this class?