2014-07-02 56 views
0

第一篇文章,我希望我似乎沒有n00bish。Java的JOptionPane.showInputDialog與構造函數

我在一個Java類中,並且遇到問題。

要求是創建一個類(Contact),該類具有獲取者和構造函數name,emailphoneNumber。然後有一個測試類(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()方法。

+1

*「而且,這三個變量,我需要(姓名,電子郵件和電話號碼)將被輸入到相同的輸入框中(由兩者之間的空格分析)。「*我認爲這是錯誤的方法,並且將複雜度提高到幾乎難以管理的水平。相反,請考慮單獨提示每個值... – MadProgrammer

+0

不允許。這項任務是設置一個輸入對話框,並通過分隔空格解析出姓名,電話號碼和電子郵件,並不斷詢問用戶是否輸入沒有數據或取消。 – ZOMGnerd

回答

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; 
} 
+0

感謝您的幫助。它看起來並不像正在使用的getter方法。此外,我不允許使用HTML:/但這給了我一些想法通過! – ZOMGnerd

+0

知道了所有的工作,不得不改變一下代碼;但你給了我正確的方向!謝謝一堆! – ZOMGnerd

0

於是我就提交了我的實驗室,這是我最終的解決方案

Contact類:

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; 
    } 
} 
+0

一般來說,除了只是一個大規模的代碼轉儲之外,它不鼓勵這樣做,它不提供有關爲什麼此解決方案解決手頭問題的信息操作。 – MadProgrammer