通过传递字符串从交换机获取信息

时间:2014-04-02 07:50:09

标签: java switch-statement parameter-passing return-value

我会尽力解释这个问题,但请你重申一下是否清楚。

我所拥有的是两个类,一个类只包含从另一个类传递给它的设置。这些类称为配置文件和设置,配置文件从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);
            }
        }
    }
}

从XML文件读取的所有设置都转到Settings类,其设置如下:

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()以格式Key:Value显示从XML文件中读取的所有设置,这显示正确。正确读取所有信息并正确显示。我可以在Profile Class和Settings类中执行此操作,看起来一切正常。

当我想要检索这些设置时,我在Profile中有一个方法:

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

这里的问题是我无法获得正确的设置。它通常会返回null或错误的值 - 其中一个设置是它是程序的第一次运行,而不是返回&#34; No&#34;它将返回&#34; 10&#34;

关于问题可能是什么的任何想法?

4 个答案:

答案 0 :(得分:4)

如果没有Break语句,则会出现Switch without Break

的所有情况

答案 1 :(得分:2)

您需要使用case语句结束每个break,否则将继续执行下一个case

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

答案 2 :(得分:0)

要添加文档摘录,

  

每个break语句都会终止封闭的switch语句。break语句是必需的,因为没有它们,switch块中的语句会通过:匹配的case标签之后的所有语句按顺序执行,而不管后续案例标签的表达,直到遇到break语句

答案 3 :(得分:0)

一般的想法是当给定条件满足时,switch语句转到特定的“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