1

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?

DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
David
  • 79
  • 1
  • 6
  • It is possible for both `getName` and `getPrice` to not find anything in the loops and therefore require a `return` statement at the end of the method – MadProgrammer Oct 15 '15 at 05:21

6 Answers6

1

Answer 1: Your return statement in getName & getPrice method is inside if block which might be executed or not based upon the condition satisfied in if hence compiler will give error.

You need to have return statement before the method returns.

Answer 2: Since all the methods in your database class are static you don't need to create object, you can invoke it directly using classname e.g.Database.getName("code")

Rahul Yadav
  • 1,503
  • 8
  • 11
0

Java and Eclipse cannot know if code will ever be equal to items[i][0]. That's why, there might not be a return value for getName.

 public static String getName(String code)
 {
     for(int i=0; i<20; i++)
     {
         if (code.equals(items[i][0]))
             return items[i][1];
     }
     return "";// or null
 }
Burkhard
  • 14,596
  • 22
  • 87
  • 108
0

With This method must return a result of type String the eclipse is saying your method return type is String so you must return a String in any case but your return statement is inside if() what if the condition inside if is never true then there is no return statement so add a default return statement to your method where you are return type String.

public static String getPrice(String code) {
    for (int i = 0; i < 20; i++) {
        if (code.equals(items[i][0])) return items[i][2];
    }
    return null;
}
singhakash
  • 7,891
  • 6
  • 31
  • 65
0

In your method getName and getPrice you have put the if condition, and method only return when your condition in true that is why you have the compilation issue.

To fix that add the else condition in which you should return or add return after completion of if block.

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
0

Your method declarations are

public static String getName(){...}
public static String getPrice(){...}

let's analyse this, static keyword means it's a static method. String means this method returns a object type of String. However you return a String array object, not a String object. Also return is inside a if statement and it the if condition not met, it won't return anything, but you must always return a object type of String.

therefore add a return after the if statement, incase if the code never enters your if block

if(YOUR CONDITION) {
     //do stuff
     //return stuff
}

//if never enters if block returns null
return null;

Your deletions must be

public static String[][] getName(){...}
public static String[][] getPrice(){...}
Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29
0

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?

The method signature states that the return type is String:

public static String getName(String code)

So you must return it. For example:

public static String getName(String code) {
String result = null;
// Perform work and update value in result.
return result;
}

Can anyone please show me an example by using this class?

You need to instantiate the class before invoking any instance method but not for static methods. For example:

String result = Database.getName("code"); //pass some string as input argument and get result.

On a side note why have you declared all methods static? You need to understand why you really need static methods.

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • Thank you, one more question if you don't mind. What about the fillArray method? How should I call the fillArray method in the main application? Is it like this: Database.fillArray(args[0])? – David Oct 15 '15 at 14:28