Skip to content

Instantly share code, notes, and snippets.

@lloricode
Created May 13, 2017 04:56
Show Gist options
  • Save lloricode/99b909ce70726a49171ae9e894dfe3f1 to your computer and use it in GitHub Desktop.
Save lloricode/99b909ce70726a49171ae9e894dfe3f1 to your computer and use it in GitHub Desktop.
sample edit view in java multi-array wtih JOptionPane
public static void main(String[] args) {
final int maximum_users = 2, maximum_columns = 4;
final int name_index = 0, address_index = 1, age_index = 2, contact_index = 3;
final String name_label = "Name: ", address_label = "Address: ", age_label = "Age: ", contact_label = "Contact: ";
final String enter_label = "Enter ";
String student_array[][] = new String[maximum_users][maximum_columns];
for (int i = 0; i < maximum_users; i++) {
student_array[i][name_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + name_label);
student_array[i][address_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + address_label);
student_array[i][age_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + age_label);
student_array[i][contact_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + contact_label);
System.out.println((i + 1) + " done.");
}
boolean loop = true;
do {
System.out.println("List of Students:");
for (int i = 0; i < maximum_users; i++) {
System.out.println((i + 1) + ". " + name_label + student_array[i][name_index] + " "
+ address_label + student_array[i][address_index] + " "
+ age_label + student_array[i][age_index] + " "
+ contact_label + student_array[i][contact_index]);
}
String option_or_search = JOptionPane.showInputDialog("1:enter to search then edit\n"
+ "2:exit");
if (option_or_search.equalsIgnoreCase("2")) {
loop = false;
System.out.println("Will exit..");
} else {
for (int i = 0; i < maximum_users; i++) {
if (option_or_search.equalsIgnoreCase(student_array[i][name_index])
|| option_or_search.equalsIgnoreCase(student_array[i][address_index])
|| option_or_search.equalsIgnoreCase(student_array[i][age_index])
|| option_or_search.equalsIgnoreCase(student_array[i][contact_index])) {
student_array[i][name_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + name_label);
student_array[i][address_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + address_label);
student_array[i][age_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + age_label);
student_array[i][contact_index] = JOptionPane.showInputDialog((i + 1) + ". " + enter_label + contact_label);
break;
}
}
}
} while (loop);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment