I will try to explain this the best I can, but please ask me to reiterate if it isn't clear.

What I have are two classes, one class only contains settings that are passed to it from the other class. These classes are called Profile and Settings, Profile reads settings from an XML file and passes it to Settings using the Key and Value as such:

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

All the settings read from the XML file go to the Settings class and is set like so:

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

Now I can use System.out.println() to display all the settings being read from the XML file in the format Key : Value and this shows correctly. All information is read correctly and displayed correctly. I can do this in both the Profile Class and the Settings class and it seems like it all works fine.

When I want to retrieve these settings I have a method in Profile as such:

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

Which goes to the method in Settings that is as such:

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

The problem here is I can't get the correct setting to be returned. It will usually return null or the wrong value - One of the Settings is if it is the first run of the program, instead of returning "No" it will return "10"

Any ideas on what the problem may be?

有帮助吗?

解决方案

Without Break statement it will goes all the case Switch without Break

其他提示

You need to end each case with a break statement, otherwise execution continues with the next case:

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

To add the excerpt from documentation,

Each break statement terminates the enclosing switch statement.The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered

The general idea is that switch statement goes to specific "case" when given condition is meet (the value from switch equals to value declared within case). The problem is that if you dont separate each case with "break;" (As Keeppil said +1 to his answer) If the application finds a case matching the value within switch it will go to that case and every case below it!

For instance:

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");

}

Will print:

12
13
14
d

but

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");

will print:

12
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top