java - How to replace the value of '0' in a given pattern? -
i new in java, , trying replace value of "0's" "x" @ index "0,1,7,13,20,21,22, , 23" in below given sequence of 0 , 1's. , code far given. problem due can not obtain desired result.
001111
101110
100010
110000
public class task { public static void main (string args[]) { file file = new file("c:\\netbeansprojects\\task\\src\\file.txt"); bufferedreader reader = null; try { reader = new bufferedreader(new filereader(file)); string text = null; //array lis declaration arraylist<string> list = new arraylist<>(); while( (text = reader.readline()) != null){ list.add(text); } //printing array list. system.out.println("print array list\n"); for(string d:list){ // system.out.println(d); } //convert arraylist 2darray int nooflines = 0; string[][] array = new string[list.size()][]; for(int i=0; < list.size(); i++){ string row = list.get(i); nooflines++; array[i] = row.split("\0"); } system.out.println("befor"); print(array); int row=0,column=0; for(int count=0; count<array.length; count++){ if(row<nooflines-1 && array[row+1][column]=="0"){ row++; } else if(column<array[row].length-1 && array[row][column+1]=="0"){ column++; } array[row][column] = "x"; } // array[0][0] = "x"; system.out.println("after"); print(array); //end try block } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } //end catch block } //end main public static void print(string[][] array){ for(int i=0; i<array.length; i++){ for(int j=0; j<array[i].length; j++){ system.out.print(array[i][j]); } system.out.println(""); } } //end function print. } //end class
one problem in line:
array[i] = row.split("\0");
the backslash means row split @ character value zero, which not character occurs in text.
if meant split row @ "0"
, use row.split("0")
. if meant split row characters, use row.split("")
.
then, using ==
compare strings. netbeans should warn not work, , suggest replace .equals
(eg., array[row+1][column].equals("0")
)
Comments
Post a Comment