java - Questions about code error detected by eclipse and creating objects to run the methods -


i newbie in java. trying create class named database read text file create array. there no main method in code since have java file acts main application, , has main method.

here 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 indicating on both methods (getname , getprice). says: " this method must return result of type string". can please explain , fix this?

question 2: database class includes methods , array created reading in text files. , have java file main application file includes main method. main application file file create object , call methods in. understand concept, keep getting errors when try create database object , call methods on object. can please show me example using class?

eclipse shows errors indicating on both methods (getname , getprice). says: " method must return result of type string". can please explain , fix this?

the method signature states return type string:

public static string getname(string code) 

so must return it. example:

public static string getname(string code) { string result = null; // perform work , update value in result. return result; } 

can please show me example using class?

you need instantiate class before invoking instance method not static methods. example:

string result = database.getname("code"); //pass string input argument , result. 

on side note why have declared methods static? need understand why need static methods.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -