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

Java 通过传递字符串从交换机获取信息,java,switch-statement,parameter-passing,return-value,Java,Switch Statement,Parameter Passing,Return Value,我会尽力解释这一点,但如果不清楚,请让我重申 我有两个类,一个类只包含从另一个类传递给它的设置。这些类称为Profile和Settings,Profile从XML文件中读取设置,并使用如下键和值将其传递给设置: public void readProfile() { // Gets our document ready to be read setProfileDoc(); // Our Root element Element root = getProfil

我会尽力解释这一点,但如果不清楚,请让我重申

我有两个类,一个类只包含从另一个类传递给它的设置。这些类称为Profile和Settings,Profile从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 String getSetting(String whatSetting)
{
    return settings.getSetting(whatSetting);
}
现在,我可以使用System.out.println以Key:Value格式显示从XML文件读取的所有设置,这将正确显示。所有信息均已正确读取和显示。我可以在Profile类和Settings类中这样做,看起来一切都很好

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

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 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或错误的值-其中一个设置是如果它是程序的第一次运行,而不是返回No,它将返回10

有什么问题吗?

没有Break语句,它将执行所有情况

没有Break语句,它将执行所有情况

您需要用Break语句结束每个情况,否则继续执行下一个情况:

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


要添加文档摘录

每个break语句都会终止封闭的switch语句。break语句是必需的,因为如果没有它们,开关块中的语句就会失效:匹配大小写标签后的所有语句都会按顺序执行,而不管后续大小写标签的表达式如何,直到遇到break语句为止


要添加文档摘录

每个break语句都会终止封闭的switch语句。break语句是必需的,因为如果没有它们,开关块中的语句就会失效:匹配大小写标签后的所有语句都会按顺序执行,而不管后续大小写标签的表达式如何,直到遇到break语句为止


一般的想法是,当给定的条件满足时,switch语句转到特定的case,switch的值等于case中声明的值。问题是,如果你不用break分开每个案例;正如Keeppil对他的答案所说的+1,如果应用程序在switch中找到一个与值匹配的case,它将转到该case,并转到它下面的每个case

例如:

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
12
但是

将打印:

12
13
14
d
12

一般的想法是,当给定的条件满足时,switch语句转到特定的case,switch的值等于case中声明的值。问题是,如果你不用break分开每个案例;正如Keeppil对他的答案所说的+1,如果应用程序在switch中找到一个与值匹配的case,它将转到该case,并转到它下面的每个case

例如:

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
12
但是

将打印:

12
13
14
d
12

从增加休息开始;到switch语句,从添加break开始;在switch语句中,我添加了break;对所有语句和第一个设置返回良好。所以FirstRun返回no,但LastProfile返回null,但我添加了break;对所有语句和第一个设置返回良好。所以FirstRun返回no,但LastProfile返回null,但我添加了break;对所有语句和第一个设置返回良好。FirstRun返回no as is should,但LastProfile返回null stillBefore set LastProfile print tempKey和TempValues的值应该是多少我选择这一个作为答案只是因为在LastProfile之前进行打印会让我找到问题-下面的所有答案都有助于解决中断问题-谢谢大家和newuserI增加了break;对所有语句和第一个设置返回良好。FirstRun返回no as is should,但LastProfile返回null stillBefore set LastProfile print tempKey和TempValues的值应该是多少我选择这一个作为答案只是因为在LastProfile之前进行打印会让我找到问题-下面的所有答案都有助于解决中断问题-谢谢大家和新用户