Search code examples
javaandroidhtmlcleaner

if else statement


I have a code with several if/else statements. However, whenever an 'else' case is thrown, all of the subsequent statements result in 'else' too. Here's the code, i'd appreciate some help please!

public List<Integer> htmlHelper(String arg) throws IOException,
        XPatherException, ParserConfigurationException,
        XPathExpressionException {
    CleanerProperties props = new CleanerProperties();

    props.setTranslateSpecialEntities(true);
    props.setTransResCharsToNCR(true);
    props.setOmitComments(true);

    HtmlCleaner cleaner = new HtmlCleaner(props);
    rootNode = cleaner.clean(arg);


    TagNode tagNode = new HtmlCleaner(props).clean(new URL(
            "http://www.athletics.psu.edu/psustrength/index_rec.asp"));
    org.w3c.dom.Document doc = (org.w3c.dom.Document) new DomSerializer(
            new CleanerProperties()).createDOM(tagNode);

    XPath jpath = XPathFactory.newInstance().newXPath();
    String recinfo = (String) jpath
            .evaluate(

"/html/body/div/div[1]/div/div/table/tbody/tr[3]/td[2]/p[2]/text()",
                    doc, XPathConstants.STRING);
    String recnum = recinfo.replaceAll("\\D+", "");

    if (recnum != null && recnum.length() == 0) {

        recnumint = 500;
        grec = 0;
    } else {
        recnumint = Integer.parseInt(recnum);
        grec = recnumint;
    }

    XPath ipath = XPathFactory.newInstance().newXPath();
    String iminfo = (String) ipath
            .evaluate(

"/html/body/div/div[3]/div/div/table/tbody/tr[2]/td[2]/p[2]/text()",
                    doc, XPathConstants.STRING);
    String imnum = iminfo.replaceAll("\\D+", "");

    if (imnum != null && recnum.length() == 0) {

        imint = 400;
        gim = 0;
    } else {
        imint = Integer.parseInt(imnum);
        gim = imint;
    }

    XPath xpath = XPathFactory.newInstance().newXPath();
    String whiteinfo = (String) xpath
            .evaluate(

"/html/body/div/div[3]/div/div/table/tbody/tr[1]/td[2]/p[2]/text()",
                    doc, XPathConstants.STRING);
    String whitenum = whiteinfo.replaceAll("\\D+", "");

    if (whitenum != null && recnum.length() == 0) {

        whitenumint = 450;
        gwhite = 0;
    } else {
        whitenumint = Integer.parseInt(whitenum);
        gwhite = whitenumint;
    }

    XPath fpath = XPathFactory.newInstance().newXPath();
    String fitinfo = (String) fpath
            .evaluate(

"/html/body/div/div[3]/div/div/table/tbody/tr[4]/td[2]/p[2]/text()",
                    doc, XPathConstants.STRING);
    String fitnum = fitinfo.replaceAll("\\D+", "");

    if (fitnum != null && recnum.length() == 0) {

        fitint = 470;
        gfit = 0;
    } else {
        fitint = Integer.parseInt(fitnum);
        gfit = fitint;
    }



    List<Integer> list = new ArrayList<Integer>();
    list.add(recnumint);
    list.add(whitenumint);
    list.add(imint);
    list.add(fitint);

    return list;
}

}

As was stated, the problem occurs when the first 'else' statement is thrown. If the first case is 'if' and the second 'else' the third and fourth will be 'else' too


Solution

  • All your ifs share the same second condition, perhaps that is the reason?

    Do you mean

    if (imnum != null && imnum .length() == 0) {
                         ^^^^^^
    

    instead of

    if (imnum != null && recnum.length() == 0) {
    

    And similarly

    if (whitenum != null && whitenum.length() == 0) {
                            ^^^^^^^^^
    

    and of course..

    if (fitnum != null && fitnum.length() == 0) {
                          ^^^^^^