I have been working on a program to help me sort through regions that meat a requirement on the website NationStates. This is my first time using any external API or XML for that matter. I have met a problem where two certain if statements at lines 87 (if (tag == "Password") {) and 91 (} else if (tag == "Minuscule") {) are being skipped even if the conditions are met. I am using eclipse and I have run through the code in debug mode with breakpoints at those lines. Please help me!
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
public static void main(final String argv[]) {
final String[] regionsarray = new String[5000];
int arraylength = 0;
try {
final URL url = new URL("http://ift.tt/13eCdSt");
final InputStream stream = url.openStream();
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
final Document doc = dBuilder.parse(stream);
doc.getDocumentElement().normalize();
final NodeList nList = doc.getElementsByTagName("EMBASSIES");
for (int temp = 0; temp < nList.getLength(); temp++) {
final Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element eElement = (Element) nNode;
for (int temp2 = 0; temp2 < 5000; temp2++)
try {
final String region = eElement.getElementsByTagName("EMBASSY").item(temp2).getTextContent();
regionsarray[arraylength] = "";
for (int temp3 = 0; temp3 < region.length(); temp3++)
if (region.charAt(temp3) == ' ')
regionsarray[arraylength] = regionsarray[arraylength] + '_';
else
regionsarray[arraylength] = regionsarray[arraylength] + region.charAt(temp3);
regionsarray[arraylength] = regionsarray[arraylength].toLowerCase();
System.out.println("Found " + regionsarray[arraylength] + ".");
arraylength++;
} catch (final NullPointerException e) {
System.out.println("END OF EMBASSY TESTING");
System.out.println();
temp2 = 5000;
}
}
}
try {
Thread.sleep(600);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
for (int temp = 0; temp < arraylength; temp++) {
boolean issuitable = true;
final URL regionurl = new URL("http://ift.tt/1J6PIEW" + regionsarray[temp] + "&q=tags");
final InputStream regionstream = regionurl.openStream();
final Document regiondoc = dBuilder.parse(regionstream);
regiondoc.getDocumentElement().normalize();
final NodeList regionnList = regiondoc.getElementsByTagName("TAGS");
for (int temp2 = 0; temp2 < regionnList.getLength(); temp2++) {
final Node regionnNode = regionnList.item(temp2);
if (regionnNode.getNodeType() == Node.ELEMENT_NODE) {
final Element regioneElement = (Element) regionnNode;
for (int temp3 = 0; temp3 < 150; temp3++)
try {
final String tag = regioneElement.getElementsByTagName("TAG").item(temp3).getTextContent();
if (tag == "Password") {
issuitable = false;
System.out.println(regionsarray[temp] + " disqualified because it is password protected.");
temp3 = 150;
} else if (tag == "Minuscule") {
issuitable = false;
System.out.println(regionsarray[temp] + " disqualified because it is too small.");
temp3 = 150;
}
} catch (final NullPointerException e) {
temp3 = 150;
}
}
}
if (issuitable)
System.out.println(regionsarray[temp] + " passed tag testing.");
try {
Thread.sleep(600);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
System.out.println("END OF TAG TESTING");
System.out.println();
} catch (final Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment