2014-04-02 62 views
1

我會盡力解釋這一點,盡我所能,但請問我重申,如果它不明確。通過傳遞字符串從交換機獲取信息

我有兩個類,一個類只包含從其他類傳遞給它的設置。這些類被稱爲配置文件和設置,配置文件從一個XML文件中讀取設置,並將其傳遞到使用鍵和值這樣設置:從XML文件中讀取

public void readProfile() 
{ 
    // Gets our document ready to be read 
    setProfileDoc(); 

    // Our Root element 
    Element root = getProfileDoc().getDocumentElement(); 

    // The name of our profile 
    Node rootAttrItem = getProfileDoc().getChildNodes().item(0); 
    Node rootAttrName = rootAttrItem.getAttributes().getNamedItem("Name"); 

    // Gets our Name value and stores into an array for later use. 
    String rootAttrValue = rootAttrName.getNodeValue(); 
    addToArray(rootAttrValue, true); 

    // Our XML file contains <Database> and <Batch> with all information in between 
    NodeList dbNodes = root.getElementsByTagName("Database"); 
    NodeList batchNodes = root.getElementsByTagName("Batch"); 

    // Run through our <Database> tags and <Batch tags> and sends the information to Settings 
    for (int x = 0; x < dbNodes.getLength(); x++) 
    { 
     Element eElement = (Element) dbNodes.item(x); 
     NodeList userInfo = eElement.getChildNodes(); 

     for (int y = 0; y < userInfo.getLength(); y++) 
     { 
      Node tempItem = userInfo.item(y); 
      if (!hasWhiteSpace(tempItem)) 
      { 
       String tempKey = tempItem.getNodeName().toString().trim(); 
       String tempValue = tempItem.getTextContent().toString().trim(); 

       settings.setAllSettings(tempKey, tempValue); 
      } 
     } 
    } 

    for (int x = 0; x < batchNodes.getLength(); x++) 
    { 
     Element eElement = (Element) batchNodes.item(x); 
     NodeList batchInfo = eElement.getChildNodes(); 

     for (int y = 0; y < batchInfo.getLength(); y++) 
     { 
      Node tempItem = batchInfo.item(y); 
      if (!hasWhiteSpace(tempItem)) 
      { 
       String tempKey = tempItem.getNodeName().toString().trim(); 
       String tempValue = tempItem.getTextContent().toString().trim(); 

       settings.setAllSettings(tempKey, tempValue); 
      } 
     } 
    } 
} 

所有的設置去設置類,是設置像這樣:

public void setAllSettings(String keyIn, String valueIn) 
{ 
    // Holds our keyIn 
    String tempKey = keyIn; 
    String tempValue = valueIn; 


    // Depending on what String is brought in, the appropriate settings will be applied 
    switch (tempKey){ 
     /* 
     * Our main settings 
     */ 
     case "FirstRun": 
      setFirstRun(tempValue); 
     case "LastProfile": 
      setLastProfile(tempValue); 
     case "LastStartedBrewName": 
      setLastStartedBrewName(tempValue); 
     case "LastStartedBrewNumber": 
      setLastStartedBrewNumber(tempValue); 
     case "LastFinishedBrewName": 
      setLastFinishedBrewName(tempValue); 
     case "LastFinishedBrewNumber": 
      setLastFinishedBrewNumber(tempValue); 
     case "CurrentBrewFile": 
      setCurrentBrewFile(tempValue); 
     case "ProfilePath": 
      setProfilePath(tempValue); 
     case "SensorFilePath": 
      setSensorFilePath(tempValue); 
     case "DBConnectionFilePath": 
      setDBConnectionFilePath(tempValue); 
     case "SensorReadIncremental": 
      setSensorReadIncremental(tempValue); 

     /* 
     * Our profile settings 
     */ 
     case "Profile Name": 
      setProfileName(tempValue); 
     case "Database Protocol": 
      setDatabaseProtocol(tempValue); 
     case "Url": 
      setDatabaseUrl(tempValue); 
     case "Port": 
      setDatabasePort(tempValue); 
     case "User": 
      setDatabaseUser(tempValue); 
     case "Pass": 
      setDatabasePass(tempValue); 
     case "Table": 
      setDatabaseTable(tempValue); 

     /* 
     * Our Batch settings 
     */ 
     case "Total": 
      setBatchTotal(tempValue); 
     case "Current": 
      setCurrentBatch(tempValue); 
    } 
} 

現在我可以使用的System.out.println()顯示從格式關鍵的XML文件中讀取所有的設置:價值與此正確顯示。所有信息被正確讀取並正確顯示。我可以在配置文件類和設置類中做到這一點,似乎它一切正常。

當我想重新使用這些設置我在個人資料的方法是這樣的:

public String getSetting(String whatSetting) 
{ 
    return settings.getSetting(whatSetting); 
} 

肚裏的方法中的設置是這樣:

public String getSetting(String getSetting) 
{ 
    String chosenValue = ""; 

    // Depending on what String is brought in, the appropriate settings will be applied 
      switch (getSetting){ 
       /* 
       * Our main settings 
       */ 
       case "FirstRun": 
        chosenValue = getFirstRun(); 
       case "LastProfile": 
        chosenValue = getLastProfile(); 
       case "LastStartedBrewName": 
        chosenValue = getLastStartedBrewName();      
       case "LastStartedBrewNumber": 
        chosenValue = getLastStartedBrewNumber();      
       case "LastFinishedBrewName": 
        chosenValue = getLastFinishedBrewName();       
       case "LastFinishedBrewNumber": 
        chosenValue = getLastFinishedBrewNumber();      
       case "CurrentBrewFile": 
        chosenValue = getCurrentBrewFile();      
       case "ProfilePath": 
        chosenValue = getProfilePath();      
       case "SensorFilePath": 
        chosenValue = getSensorFilePath();      
       case "DBConnectionFilePath": 
        chosenValue = getDBConnectionFilePath();       
       case "SensorReadIncremental": 
        chosenValue = getSensorReadIncremental();      

       /* 
       * Our profile settings 
       */ 
       case "Profile Name": 
        chosenValue = getProfileName();      
       case "Database Protocol": 
        chosenValue = getDatabaseProtocol();       
       case "Url": 
        chosenValue = getDatabaseUrl();      
       case "Port": 
        chosenValue = getDatabasePort();       
       case "User": 
        chosenValue = getDatabaseUser();       
       case "Pass": 
        chosenValue = getDatabasePass();       
       case "Table": 
        chosenValue = getDatabaseTable();      

       /* 
       * Our Batch settings 
       */ 
       case "Total": 
        chosenValue = getBatchTotal();      
       case "Current": 
        chosenValue = getCurrentBatch(); 
      } 

    return chosenValue; 
} 

這裏的問題是我無法獲得正確的返回設置。它通常會返回空值或錯誤的值 - 其中一個設置是如果它是該程序的第一次運行,而不是返回「否」,將返回「10」

有關這個問題可能有什麼想法?

+0

開始增加「打破」;到switch語句 – Taks

回答

4

沒有Break聲明它會去所有Switch without Break

+0

我加了break;所有的陳述和非常拳頭設置返回罰款。因此,FirstRun返回不應該如此,但LastProfile仍然返回空 – adamb53

+1

在設置「LastProfile」之前打印什麼應該是tempKey和tempValue的價值 – newuser

+0

我選擇這一個作爲答案只是因爲在LastProfile之前打印導致我找到問題 - 所有的答案都在幫助解決問題 - 謝謝大家和新用戶 – adamb53

2

你需要結束每個casebreak聲明的情況下,否則繼續執行下一case

switch (getSetting){ 
    /* 
    * Our main settings 
    */ 
    case "FirstRun": 
     chosenValue = getFirstRun(); 
     break; 
    case "LastProfile": 
     chosenValue = getLastProfile(); 
     break; 
    ... 
+0

我已添加break;所有的陳述和非常拳頭設置返回罰款。所以FirstRun返回no,但是LastProfile仍然返回null – adamb53

0

從文檔添加摘錄,

每個break語句終止封閉的switch語句。 break語句是必要的,因爲沒有他們,在開關塊告吹聲明:匹配的case標籤後,所有語句順序執行,而不管後續的情況下標籤的表達,直到遇到一個break語句

0

總體思路是,當給定的條件滿足時(開關的值等於case內聲明的值),switch語句轉到特定的「case」。問題是,如果你不把每個案件與「休息」分開, (正如Keeppil對他的答案所說的+1)如果應用程序發現一個與switch內的值匹配的案例,它將轉到該案例和它下面的每個案例!

例如:

int w=12; 
switch(w) { 
case 1: 
    System.out.println("1"); 
case 2: 
    System.out.println("2"); 
case 3: 
    System.out.println("3"); 
case 12: 
    System.out.println("12"); 
case 13: 
    System.out.println("13"); 
case 14: 
    System.out.println("14"); 
    default: 
     System.out.println("d"); 

} 

會打印:

12 
13 
14 
d 

switch(w) { 
case 1: 
    System.out.println("1"); 
    break; 
case 2: 
    System.out.println("2"); 
    break; 
case 3: 
    System.out.println("3"); 
    break; 
case 12: 
    System.out.println("12"); 
    break; 
case 13: 
    System.out.println("13"); 
    break; 
case 14: 
    System.out.println("14"); 
    break; 
    default: 
     System.out.println("d"); 

會打印:

12