Change if you still need to switch to the line

How to write the following method using switch caseinsteadif else

public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException 
{

    currentElement = true;

    if (localName.equals("maintag"))
    {
        /** Start */ 
        sitesList = new SitesList();
    } else if (localName.equals("website")) {
        /** Get attribute value */
        String attr = attributes.getValue("category");
        sitesList.setCategory(attr);
    }

}
+3
source share
9 answers

In your case, you should use ENUM, not a string literal, and then you can refer to the ENUMcase in the switch:

Sitelink

+7
source

String Switching

You can not. Java 1.6 does not support switch statements with strings.

Java 1.7 is possible (this is one of the features of Project Coin ), but for 1.6 you need to either use an enumeration (the best choice) or otherwise use a map.

:

:

interface SiteListProvider{
    SitesList provide();
}

, SiteListProvider:

Map<String, SiteListProvider> providers =
    new HashMap<String, SiteListProvider>();
providers.put("foo", new SiteListProvider(){
    public SitesList provide(){
        return new SiteList("foo", "bar", "baz");
    }

});
providers.put("phleem", new SiteListProvider(){
    public SitesList provide(){
        return new SiteList("otherstuff");
    }
});

:

SiteList siteList = providers.get(localName).provide();
+5

Java 7 , enum, .

enum LocalName {
    maintag, website, UNKNOWN;
    public static LocalName lookup(String text) {
        try {
            return valueOf(text);
        } catch(Exception e) {
            return UNKNOWN;
        }
     }
}

switch(LocalName.lookup(localName)) {
    case maintag:
        /** Start */ 
        sitesList = new SitesList();
        break;
    case website:
        /** Get attribute value */
        String attr = attributes.getValue("category");
        sitesList.setCategory(attr);
        break;
}
+1

localName String, . switch byte, short, char, int ().

0

if else, :

, , char int . ( ) , "", : , Byte, Short Integer ( ).

: http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

0

, , :

public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException 
        {

        currentElement = true
        switch(localName.charAt(0)) {
           case 'm':
               sitesList = new SitesList();
               break;
           case 'w':
               String attr = attributes.getValue("category");
               // etc.
               break;
           default:
               break;
        }
        ...
}

, , ! , AN ; -)

0

If you can replace the string with an enumeration, do it - as already mentioned. If you cannot, you can get an enumeration for the string, as shown below:

Declaration and Initialization

enum LocalName {MAINTAG, WEBSITE}
private final static Map<String, LocalName> localNameForString
    = new HashMap<String, LocalName>();
static {
    for (LocalName n : LocalName.values()) {
        localNameForString.put(n.name().toLowerCase(), n);
    }
}

Using

LocalName n = localNameForString.get(localName);
if (n==null) {
   // do something
} else  switch (n) {
    ...
}

This is a bit verbose, but not very bad, and working with enums is a pleasure .: D

0
source

I solve this problem by changing the "switch" to "if" something like this:

if (mystring.equals("string")
   thisstring="vi";
0
source

Source: https://habr.com/ru/post/1787414/


All Articles