2016-11-24 49 views
-1

我正在做一個程序,它返回一個長字符串變量的輸出。字符串中的數據會根據用戶在GUI中輸入的內容而不斷變化。我的問題是,我如何把它存儲在我的鏈表中?我已經看了幾個例子,但是我爲我的班級提供的班級有點不同,而且我一直無法找到解決問題的方法。將字符串變量添加到鏈接列表?

控制器代碼:

public class RentGameDialogController extends RentalStoreGUIController implements Initializable{ 

/** TextField Objects **/ 
@FXML private TextField nameField, rentedOnField, dueBackField; 

/** String for NameField **/ 
String name, rentedOn, dueBack; 

/** Game ComboBox ID's **/ 
@FXML private ObservableList<GameType> cbGameOptions; 
@FXML private ComboBox<GameType> cbGame; 

/** Console ComboBox ID's **/ 
@FXML private ObservableList<PlayerType> cbConsoleOptions; 
@FXML private ComboBox<PlayerType> cbConsole; 

/** GameType object **/ 
private GameType game; 

/** PlayerType Object **/ 
private PlayerType console; 

/** Button ID's **/ 
@FXML Button cancel, addToCart; 

/** Counter for calculating total **/ 
int gameCounter; 

/** Stage for closing GUI **/ 
private Stage currentStage; 

private MyLinkedList list = new MyLinkedList(); 




@Override 
public void initialize(URL location, ResourceBundle resources) { 

    /** Select Console **/ 
    cbConsoleOptions = FXCollections.observableArrayList(); 
    for (PlayerType p : PlayerType.values()) { cbConsoleOptions.addAll(p); } 
    cbConsole.getItems().addAll(cbConsoleOptions); 

    /** Select Game **/ 
    cbGameOptions = FXCollections.observableArrayList(); 
    for (GameType g : GameType.values()){ cbGameOptions.addAll(g); } 
    cbGame.getItems().addAll(cbGameOptions); 

} 

public String getName(){ 
    name = nameField.getText(); 

    try { 

     String[] firstLast = name.split(" "); 
     String firstName = firstLast[0]; 
     String lastName = firstLast[1]; 

    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

    return name; 
} 

public String getGame() { 
    return cbGame.getSelectionModel().getSelectedItem().toString(); 
} 

public String getConsole() { 
    return cbConsole.getSelectionModel().getSelectedItem().toString(); 
} 

public String getRentedOn() throws ParseException { 

    rentedOn = rentedOnField.getText(); 


    DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
    Date rentedOnDate = format.parse(rentedOn); 

    Calendar cal = Calendar.getInstance(); 
    cal.setLenient(false); 
    cal.setTime(rentedOnDate); 

    try { 

     cal.getTime(); 

    } catch (Exception e) { 
     rentedOnField.setText("ERROR"); 
    } 

    return rentedOn; 

} 

public String getDueBack() throws ParseException { 

    dueBack = dueBackField.getText(); 


    DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
    Date dueBackDate = format.parse(dueBack); 

    Calendar cal = Calendar.getInstance(); 
    cal.setLenient(false); 
    cal.setTime(dueBackDate); 

    try { 

     cal.getTime(); 

    } catch (Exception e) { 
     dueBackField.setText("ERROR"); 
    } 

    return dueBack; 

} 

/************************************* 
* This is the method to call the other 
* String methods so their output can be 
* put into my main GUI 
* 
* 
* @return 
* @throws ParseException 
*************************************/ 

public String storePurchaseData() throws ParseException { 
    gameCounter++; 
    String toList = getName() + " | " + getGame() + " | " + getConsole() + " | " + 
      getRentedOn() + " | " + getDueBack(); 

    //Add 'toList' to the linked list here if possible 

    return toList; 
} 


@FXML 
public void handleCancelButtonAction() { 
    currentStage = (Stage) cancel.getScene().getWindow(); 
    currentStage.close(); 
} 

@FXML 
public void addToCartButton() throws ParseException { 
    appendTextArea(storePurchaseData()); 
    currentStage = (Stage) cancel.getScene().getWindow(); 
    currentStage.close(); 
}} 

此代碼是我的控制器。它啓動一個基本的GUI,然後我可以從我製作的所有字段中提取數據,將它們轉換爲字符串,然後可以將它們打印在一長串文本中。我想將字符串存儲到我的鏈接列表類中。

鏈表代碼:

public class MyLinkedList<E> implements Serializable { 

private DNode<E> top; 
public int size; 

public MyLinkedList() { 
    top = null; 
    size = 0; 
} 

}

我很新的鏈表,我想了解他們,這段代碼有意義嗎?我是否需要添加任何內容,比如說將我存儲的字符串保存到文本文件中?

預先感謝您

+0

我看到的一個可能的問題是,您沒有在MyLinkedList類中初始化變量top。 –

+1

考慮提供[_Minimal_,Complete和Verifiable示例](http://stackoverflow.com/help/mcve)。我不想篩選你的所有代碼來找到相關的位 – qxz

回答

1

沒有進入你的遊戲代碼的話,那看起來像你的MyLinkedList類需要一個類型參數E - 你還沒有顯示的代碼DNode,但它也需要在E類型。如果您可以指定這是一個String,那麼MyLinkedList的節點可以根據需要填充字符串。

DNode<String> myFirstNode = new DNode<>(null, null, "nodeData"); 
MyLinkedList<String> list = new MyLinkedList<>(myFirstNode); 

這假定MyLinkedList類也有一個構造函數一個DNode初始化其head,而DNode看起來像this

+2

提示:它應該讀取'新的DNode <>(...'否則你正在創建一個**原始類型**,這將是值得的downvote;同樣,你不需要新的MyLinkedList的類型名稱......空鑽石操作員做這項工作! – GhostCat

+0

謝謝......自從我寫了任何Java以來​​已經有一段時間了! – retrospectacus

+0

非常歡迎。 – GhostCat