String Array class in JAVA -


i need code

problem

  1. write java class having string array, global visibility.
  2. add method adds given sting string array.
  3. add method searches given string in string array.
  4. add method searches given character in string array. method should count , returns occurrence of given character.
  5. write appropriate main method test these class methods.

and code. first, created class method create scound class teststring array

my question have error in scound class ,and try fix dose not work first class:

 /*      * change template, choose tools | templates      * , open template in editor.      */     package ooplab3;      public class stringarray {          string[] sta = null;         int index = 0; //last added sring position in string array          public stringarray() {         }          public string[] getsta() {             return sta;         }          public string getstaindex(int i) {             return sta[i];         }          public int getcounter() {             return index;         }          public void setcounter(int counter) {             this.index = counter;         }          public void addstrintoarray(string st) {             if (this.index < sta.length) {                 sta[this.index] = st;                 this.index++;             }         }          public int searchstringinarray(string st) {             int n = 0;             (int = 0; < this.index; i++) {                 (int j = 0; j < 10; j++) {                     int indexof = sta[i].indexof(st);                     n += searchstringinarray(sta[i]);                     return n;                 }             }             return n;         }          public int searchcharinarray(string st) {             int n = 0;             int startindex = 0;             {                 n += st.indexof(startindex);             } while (n > startindex);             return n;         }          public boolean containschar(string s, char search) {             if (s.length() == 0) {                 return false;             } else {                 return s.charat(0) == search || containschar(s.substring(1), search);             }         }          public void containschar(object object, string search) {         }       } 

sound class :

/*  * change template, choose tools | templates  * , open template in editor.  */ package ooplab3;  import java.io.filenotfoundexception; import java.io.filereader; import java.util.scanner;  public class teststringarray {       public static void main(string[] args) throws filenotfoundexception {         string[] testarray = new string[30];         scanner infile = new scanner(new filereader("input_txt"));          // system.out.println("contents of testarray");          int = 0;         while (infile.hasnext()) {             string j = infile.next();             addstring(j, i);              system.out.println(testarray[i] + "\n");              i++;         }         }       }       input file contain: hello java program   

as near can tell, have class named stringarray, , second class intended test capabilities of stringarray. second class doesn't use stringarray @ all; instead, creates own array, , calls method addstring() has similar, not identical, name method in stringarray.

one significant problem stringarray never creates actual array -- array member variable remains null. need "new" expression create actual array. test class should doing like

stringarray sa = new stringarray(); sa.addstringtoarray("hello, world"); string[] array = sa.getsta(); (string s: array)     system.out.println(s); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -