如果我想检测用户是否输入了任何字符,我需要在 try catch block 中添加什么异常?
这是我的代码,我想知道用户是否没有输入任何内容,或者用户的输入是否已保存在地址簿
中。我正在使用数组来存储我的条目:
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++;
}
我需要 try-catch 还是条件?这是我的完整代码:
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();
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: ");
for (int i = 0; i < counter; i++) {
if (entry[i].getName().equals(SName)) {
JOptionPane.showMessageDialog(null, "Found!");
entry[i] = null;
break;
}
}
}
}
我的 addEntry()
方法有问题,因为我想检测用户新添加的条目是否已存储在我的地址簿中,以及当我询问时用户是否未输入任何内容在 JOptionPane 中“输入名称:”,然后仍然按“确定”。
最佳答案
虽然不太清楚你的问题,但我会尽力回答。
在这种情况下,您可以在此处手动创建自己的异常,例如,
throw new MyException();
由于您的条件“用户没有输入任何字符”本身并不清晰,我建议您可以尝试通过检查参数的数量来抛出异常通过命令行传递。
如果它是 0,即没有传递任何参数,您可以抛出自己的异常。
但就其本身而言,不会引发异常。
关于java - Java中有没有检测到没有输入的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048593/