Как искать и удалять входы, используя JOptionPane, и как заставить программу вернуться к началу, когда пользователь нажимает да после «Вы хотите повторить попытку»?
Первая часть кода - это то, куда идут входы. Я не уверен, что OP нужно что-то здесь вставить, чтобы помочь с удалением, потому что позже мне придется заставить его удалить один из входных данных.
Java:
int i = 0;
while (i < numItems && inp != null) {
inp = JOptionPane.showInputDialog(null, "Enter name of expense " + i);
if (inp != null) {
expenseNameList[i] = inp;
inp = JOptionPane.showInputDialog(null, "Enter price of " + expenseNameList[i]);
if (inp != null) {
expensePriceList[i]=Double.parseDouble(inp);
}
}
i = i + 1;
}
if (inp == null) {
return;
}
Во второй части кода я должен добавить опцию, чтобы пользователь мог искать и удалять входные данные из массива.
//searching for an expense
String target = "";
msg = "";
while (msg.length() == 0 && target != null) {
target=JOptionPane.showInputDialog("Which expense did you want to search for...");
if (target != null) {
i = 0;
while(i < numItems) {
if (expenseNameList[i].contains(target)) {
msg = msg + expenseNameList[i] + ": " + expensePriceList[i] + "\n";
}
i = i + 1;
}
}
if (msg.length() > 0) {
JOptionPane.showMessageDialog(null, "Results found ...\n" + msg);;
{
int reply = JOptionPane.showConfirmDialog(null, "Do you want to try again?", "", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
JOptionPane.showMessageDialog(null, "Hello");
//need to make it go back to the start here
else {
JOptionPane.showMessageDialog(null, "Goodbye!");