第一篇文章,我希望我似乎并不喜欢。
我在Java课程中遇到问题。
要求是创建一个具有getter的类(Contact
)以及name
,email
和phoneNumber
的构造函数。然后是一个测试类(TestContact
),其while loop
一直提示用户,因为点击OK
并且没有输入任何内容,点击Enter或者名称超过21个字符。
此外,我需要的三个变量(姓名,电子邮件和电话号码)将输入到同一个输入框中(由空格解析)。
我似乎无法弄清楚如何让它发挥作用。我有很多错误。
首先,我不确定如何设置数组,然后用空格分割,然后使用该数组设置我的变量&吸气鬼(希望有道理?)。
此外,由于数组中的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];
}
}
我的问题现在仅在parsing()方法中。
答案 0 :(得分:0)
有两个主要错误......
在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;
}
答案 1 :(得分:0)
所以我刚刚提交了我的实验室,这是我的最终解决方案
联系班级:
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;
}
}