How To Search Value In A 2D Array Using Java NetBeans
In this Java Tutorial we will see How To Search And Find A Value Position Inside A Multidimensional Array Using For Loop And Display The Position Into A JTextArea On A JButton Click Event In Java NetBeans .
Project Source Code:
// the array
String[][] data = {
{"A1","B1","C1","D1","E","F","G"},
{"A2","B2","C2","D2","E","B1","G"},
{"A3","B3","C3","B1","E","F","G"},
{"A4","B4","B1","D4"},
{"A5","B5","C5","D5"},
{"A6","B6","C6","B1"},
{"B1","B7","C7","D7"},
};
// show array data in textArea
public void showArrayData()
{
String txt = "";
for(int i = 0; i < data.length; i++){
for(int c = 0; c < data[i].length; c++){
//jTextArea1.setText(jTextArea1.getText() + "");
txt = txt + data[i][c] + " , ";
}
txt = txt + "\n";
}
jTextAreaData.setText(txt);
}
// get positions
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String pos = "", valToSearch = jTextField1.getText();
for(int i = 0; i < data.length; i++){
for(int j = 0; j < data[i].length; j++)
{
if(data[i][j].equals(valToSearch)){
pos = pos + "POSITION [ "+i+" , "+j+" ]";
pos = pos + "\n";
}
}
}
jTextAreaPos.setText(pos);
}
Post A Comment:
0 comments: