My aim is to fill a 3x3 grid of Jtextfield in java swing with values and position of entry in that grid sourced from a text file.I made a file named aa.txt. Its contents are:-
a1-3
b3-9
c2-4
a1,b3,c2,... are names of textfields in grid and numbers separated by hyphen are the values i need to enter in the respective field.
I wrote a code for that but it is not working as expected. Instead of going through all the entries in text file, the code is just filling data into first box for entry i.e a1 only gets filled and rest of them are not filled.
There is no syntatical error(or ArrayOutOfBounds exception); the only error is i cannot get the desired result.
File f= new File("C:\\Users\\Utkarsh\\Desktop\\aa.txt");
String[] posval=new String[5];int c=0;
Scanner s= new Scanner(f);
while(s.hasNext()){
posval[c]=s.nextLine();
c++;
}
String[] pos= new String[5];
String[] val= new String[5];
for(int j=0;j<3;j++){
String[] temp=posval[j].split("-");
pos[j]= temp[0];
val[j]=temp[1];
}
JTextField[][] a={{a1,a2,a3},{b1,b2,b3},{c1,c2,c3}};
String[][] d={{"a1","a2","a3"},{"b1","b2","b3"},{"c1","c2","c3"}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(pos[j].equals(d[i][j])){
a[i][j].setText(""+val[j]);
}
}
}
Please tell me where i am going wrong. Thanks.
If u have a better algorithm to do the task, please tell me about it.