第一篇文章,我希望我似乎沒有n00bish。Java的JOptionPane.showInputDialog與構造函數
我在一個Java類中,並且遇到問題。
要求是創建一個類(Contact
),該類具有獲取者和構造函數name
,email
和phoneNumber
。然後有一個測試類(TestContact
),其中有一個while loop
,因爲命中OK
而不斷鍵入提示用戶,並且沒有輸入任何內容,所以按Enter或名稱超過21個字符。
此外,我需要的三個變量(姓名,電子郵件和電話號碼)將被輸入到相同的輸入框中(由兩者之間的空白符分隔)。
我似乎無法弄清楚如何讓它工作。我有很多錯誤。首先,我不知道如何設置數組,然後用空白符分割它,然後使用該數組來設置我的變量& getters(希望有意義?)。
此外,由於NullPointerException
中的數組和索引超出範圍異常,程序不斷崩潰。
Contact類:
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()方法。
*「而且,這三個變量,我需要(姓名,電子郵件和電話號碼)將被輸入到相同的輸入框中(由兩者之間的空格分析)。「*我認爲這是錯誤的方法,並且將複雜度提高到幾乎難以管理的水平。相反,請考慮單獨提示每個值... – MadProgrammer
不允許。這項任務是設置一個輸入對話框,並通過分隔空格解析出姓名,電話號碼和電子郵件,並不斷詢問用戶是否輸入沒有數據或取消。 – ZOMGnerd