第一个帖子,我希望我没有显得很无聊。
我在一个Java类,我卡住了一个问题。
要求创建一个类(Contact
),该类具有name
、email
和phoneNumber
的getter和构造函数。然后是一个测试类(TestContact
),它有一个while loop
,它不断提示用户,因为点击了OK
而没有输入任何内容,点击Enter或名称超过21个字符。
同样,我需要的三个变量(姓名、电子邮件和电话号码)将被输入到同一个输入框中(通过空格进行解析)。
我似乎不知道如何使它工作。我有很多虫子。
首先,我不确定如何设置数组,然后用空格分割它,然后使用该数组设置变量&getter(希望这能理解?)。
此外,由于数组中的NullPointerException
和数组索引越界异常,程序不断崩溃。
联系类:
public class Contact
{
//Initiating variables
private String name;
private String phoneNumber;
private String eMail;
//Constructor
public Contact()
{
this.name = getName();
this.phoneNumber = getPhoneNumber();
this.eMail = getEMail();
}
//Getter for name variable
public String getName()
{
return name;
}
//Getter for phoneNumber variable
public String getPhoneNumber()
{
return phoneNumber;
}
//Getter for eMail variable
public String getEMail()
{
return eMail;
}
}
TestContact类:
public class testContact
{
public static void main(String[] args)
{
Contact myContact = new Contact();
String userInput;
String noUserInput;
userInput = JOptionPane.showInputDialog("Please enter your First and Last Name, Phone Number, & E-mail: ");
do
{
String[] phrases = userInput.split(" ");
String name = phrases[0] + " " + phrases[1];
String phoneNumber = phrases[2];
String eMail = phrases[3];
if (!userInput.equals(""))
{
if (name.length() > 21)
{
String userInput = JOptionPane.showInputDialog("I'm sorry but your name is too long.nPlease enter your First and Last Name, Phone Number, & E-mail: ");
String[] phrases = userInput.split(" ");
String name = phrases[0] + " " + phrases[1];
String phoneNumber = phrases[2];
String eMail = phrases[3];
JOptionPane.showMessageDialog(null, "Name: "+myContact.getName()+"nPhone Number: "+myContact.getPhoneNumber()+"nE-Mail: "+myContact.getEMail());
}
else
{
JOptionPane.showMessageDialog(null, "Name: "+name+"nPhone Number: "+phoneNumber+"nE-Mail: "+eMail);
}
}
while ((userInput = JOptionPane.showInputDialog("I'm sorry but you didn't enter anything.nPlease enter your First and Last Name, Phone Number, & E-mail: ")) == null)
{
String[] phrases = userInput.split(" ");
String name = phrases[0] + " " + phrases[1];
String phone = phrases[2];
String eMail = phrases[3];
JOptionPane.showMessageDialog(null, "Name: "+myContact.getName()+"nPhone Number: "+myContact.getPhoneNumber()+"nE-Mail: "+myContact.getEMail());
}
}while(userInput != null);
}
}
注意我改变了TestContact类的周围,使它看起来更漂亮一些,如下图所示。我唯一的问题是如何设置我从字符串数组解析的方法,并放入字符串变量。我如何为构造函数设置这些??
public class testContact
{
static String userInput;
static Contact myContact = new Contact();
public static void main(String[] args)
{
do
{
parsing(initialInput());
if (!userInput.equals(""))
{
if (myContact.getName().length() > 21)
{
parsing(nameLengthErrorInput());
output();
}
else
{
output();
}
}
else
{
parsing(nullErrorInput());
output();
}
}while(userInput != null);
}
public static String initialInput()
{
userInput = JOptionPane.showInputDialog("Please enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
public static String nameLengthErrorInput()
{
userInput = JOptionPane.showInputDialog("I'm sorry but your name is too long.nPlease enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
public static String nullErrorInput()
{
userInput = JOptionPane.showInputDialog("I'm sorry but you didn't enter anything.nPlease enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
public static void output()
{
JOptionPane.showMessageDialog(null, "Name: "+myContact.getName()+"nPhone Number: "+myContact.getPhoneNumber()+"nE-Mail: "+myContact.getEMail());
}
public static void parsing(String userInput)
{
String[] phrases = userInput.split(" ");
String name = phrases[0] + " " + phrases[1];
String phoneNumber = phrases[2];
String eMail = phrases[3];
}
}
我的问题现在是唯一的解析()方法。
有两个主要的错误…
第一…
在Contact
构造函数中,您将变量赋值给它们自己…
public Contact() {
this.name = getName();
this.phoneNumber = getPhoneNumber();
this.eMail = getEMail();
}
这几乎等同于说…
public Contact() {
this.name = this.name;
this.phoneNumber = this.phoneNumber;
this.eMail = this.eMail;
}
结果是一样的…
第二…
你在这里得到一个NullPointerException
…
if (myContact.getName().length() > 21) {
因为Contact#getName
的值从未被赋值(一个有效的)
建议…
第一…
我建议改变Contact
构造函数,要求值传递给它…
public Contact(String name, String phoneNumber, String eMail) {
this.name = name;
this.phoneNumber = phoneNumber;
this.eMail = eMail;
}
这将意味着…
static Contact myContact = new Contact();
将不再编译,但您可以将其更改为
static Contact myContact;
相反……
第二…
我建议更改您的parsing
方法以返回Contact
,例如…
public static Contact parsing(String userInput) {
Contact contact = null;
if (userInput != null && userInput.trim().length() > 0) {
String[] phrases = userInput.split(" ");
if (phrases.length == 4) {
String name = phrases[0] + " " + phrases[1];
String phoneNumber = phrases[2];
String eMail = phrases[3];
contact = new Contact(name, phoneNumber, eMail);
}
}
return contact;
}
您还应该防止无效输入。
这意味着每次使用解析方法时都需要分配结果…
myContact = parsing(initialInput());
第三…
每次用户输入失败时,你应该简单地显示一个错误信息,让他们输入新的信息,你在尝试这样做,但是你没有利用现有的错误检查来重新验证输入…
public static void main(String[] args) {
String errorMsg = "";
do {
myContact = parsing(getInput(errorMsg));
if (myContact != null) {
if (myContact.getName().length() > 21) {
myContact = null;
errorMsg = "<html>I'm sorry but your name is too long.<br>";
}
} else {
errorMsg = "<html>I'm sorry but you didn't enter anything.<br>";
}
} while (myContact == null);
output();
}
public static String getInput(String errorMsg) {
userInput = JOptionPane.showInputDialog(errorMsg + "Please enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
我刚刚提交了我的实验结果这是我的最终解决方案
联系类:
public class Contact
{
private String name;
private String phoneNumber;
private String eMail;
public Contact(String name, String phoneNumber, String eMail)
{
this.name = name;
this.phoneNumber = phoneNumber;
this.eMail = eMail;
}
public String getName()
{
return name;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public String getEMail()
{
return eMail;
}
}
TestContact类:
import javax.swing.JOptionPane;
public class testContact
{
static Contact myContact;
static Boolean exitBool = true;
public static void main(String[] args)
{
myContact = parsing(initialInput());
do
{
if (myContact != null)
{
if (myContact.getName().length() > 21)
{
myContact = parsing(nameLengthErrorInput());
exitBool = false;
}
else
{
exitBool = true;
}
}
else if (myContact == null)
{
myContact = parsing(nullErrorInput());
exitBool = false;
//output();
}
}while(exitBool == false);
output();
}
public static String initialInput()
{
userInput = JOptionPane.showInputDialog("Please enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
public static String nameLengthErrorInput()
{
userInput = JOptionPane.showInputDialog("I'm sorry but your name is too long.nPlease enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
public static String nullErrorInput()
{
userInput = JOptionPane.showInputDialog("I'm sorry but you didn't enter anything.nPlease enter your First and Last Name, Phone Number, & E-mail: ");
return userInput;
}
public static void output()
{
JOptionPane.showMessageDialog(null, "Name: "+myContact.getName()+"nPhone Number: "+myContact.getPhoneNumber()+"nE-Mail: "+myContact.getEMail());
}
public static Contact parsing(String userInput) {
Contact contact = null;
if (userInput != null && userInput.trim().length() > 0) {
String[] phrases = userInput.split(" ");
String name = phrases[0] + " " + phrases[1];
String phoneNumber = phrases[2];
String eMail = phrases[3];
contact = new Contact(name, phoneNumber, eMail);
}
return contact;
}
}