gpt4 book ai didi

java - 在Java中将其添加和存储在数组中之前如何检查和输入?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:38:51 25 4
gpt4 key购买 nike

美好的一天..我有一个地址簿程序...它运行正常但它不检查用户输入是否已经存储在数组中..我想做的是......在获得用户之后输入将它与数组中存储的条目进行比较,当它是唯一的时……它将允许用户添加一个新条目……

这是我在 addEntry() 中的旧代码:

public void addEntry() {

entry[counter] = new AddressBookEntry();
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
counter++;

这是我打算做的,但结果是错误的:

public void addEntry() {

    entry[counter] = new AddressBookEntry();
SName = JOptionPane.showInputDialog("Enter name: ");//<-- asks user for the name
if (!entry[counter].getName().equals(SName)) {//<--compare
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
counter++;
}
}

这是错误:线程“main”中的异常 java.lang.NullPointerException 在 AddressBook.addEntry(AddressBook.java:57) 在 AddressBook.main(AddressBook.java:28)Java 结果:1构建成功(总时间:4 秒)

输出只要求用户输入一个名字然后自动退出...这可能是一个逻辑错误......但我不知道可能的解决方案是什么

需要帮助谢谢

这是我的完整代码:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class AddressBook {

private AddressBookEntry entry[];
private int counter;
private String SName;
private int notfound = 0;

public static void main(String[] args) {
AddressBook a = new AddressBook();
a.entry = new AddressBookEntry[100];
int option = 0;
try {
while (option != 5) {
String content = "Choose an Option\n\n"
+ "[1] Add an Entry\n"
+ "[2] Delete an Entry\n"
+ "[3] Update an Entry\n"
+ "[4] View all Entries\n"
+ "[5] View Specific Entry\n"
+ "[6] Exit";
option = Integer.parseInt(JOptionPane.showInputDialog(content));
switch (option) {
case 1:
a.addEntry();
break;
case 2:
a.deleteEntry();
break;
case 3:
a.editEntry();
break;
case 4:
a.viewAll();
break;
case 5:
a.searchEntry();
break;
case 6:
System.exit(1);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice!");
}
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please Choose a Number in the displayed Menu");
}
}

public void addEntry() {
entry[counter] = new AddressBookEntry();
SName = JOptionPane.showInputDialog("Enter name: ");
if (entry[counter] == null && entry[counter].getName() == null
&& !entry[counter].getName().equals(SName)) {
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
counter++;
}
}
/*public void addEntry() {
entry[counter] = new AddressBookEntry();
entry[counter].setName(JOptionPane.showInputDialog("Enter name: "));
entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: "));
entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: "));
entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: "));
counter++;
}*/

public void viewAll() {
String addText = " NAME\tADDRESS\tPHONE NO.\tE-MAIL ADD\n\n";
int nonNull = 0;
for (int i = 0; i < entry.length; i++) {
if (entry[i] != null) {
addText = addText + entry[i].getInfo() + "\n";
nonNull++;
}
if (nonNull == counter) {
break;
}
}
JOptionPane.showMessageDialog(null, new JTextArea(addText));
}

public void searchEntry() {
SName = JOptionPane.showInputDialog("Enter Name to find: ");
searchMethod();
}

public void searchMethod() {
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(SName)) {
JOptionPane.showMessageDialog(null, entry[i].getInfo2());
notfound = 0;
break;
} else {
notfound++;
}
}
if (notfound != 0) {
JOptionPane.showMessageDialog(null, "Name Not Found!");
}
}

public void editEntry() {
SName = JOptionPane.showInputDialog("Enter Name to edit: ");
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(SName)) {
entry[i] = new AddressBookEntry();
entry[i].setName(JOptionPane.showInputDialog("Enter new name: "));
entry[i].setAdd(JOptionPane.showInputDialog("Enter new add: "));
entry[i].setPhoneNo(JOptionPane.showInputDialog("Enter new Phone No.: "));
entry[i].setEmail(JOptionPane.showInputDialog("Enter new E-mail: "));
notfound = 0;
break;
} else {
notfound++;
}
}
if (notfound != 0) {
JOptionPane.showMessageDialog(null, "Name Not Found!");
}
}

public void deleteEntry() {
SName = JOptionPane.showInputDialog("Enter Name to delete: ");
if (SName == null) {
return;
}
for (int i = 0; i < counter; i++) {
if (entry[i] != null && SName.equals(entry[i].getName())) {
entry[i] = null;
JOptionPane.showMessageDialog(null, "Found!");
break;
}
}
}
}

希望你能帮助我...因为我仍然不明白该怎么做。所以我只展示我的完整代码...感谢你们的耐心等待...

最佳答案

在尝试从中执行 getName() 之前,请检查以确保您的条目不为空。

if (!entry[counter].getName().equals(SName))

变成:

if  (entry[counter] != null 
&& entry[counter].getName() != null
&& !entry[counter].getName().equals(SName))

关于java - 在Java中将其添加和存储在数组中之前如何检查和输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5049044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com