Rosetta Code/Count examples: Difference between revisions
Content added Content deleted
(Adjusted length of lines to approx. 80 chars) |
|||
Line 608: | Line 608: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{lines_too_long|Java}} |
|||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
<lang java5> |
<lang java5> |
||
Line 616: | Line 615: | ||
public class CountProgramExamples { |
public class CountProgramExamples { |
||
private static final String baseURL = "http://rosettacode.org/wiki/"; |
private static final String baseURL = "http://rosettacode.org/wiki/"; |
||
private static final String rootURL = "http://www.rosettacode.org/w/ |
private static final String rootURL = "http://www.rosettacode.org/w/" |
||
+ "api.php?action=query&list=categorymembers" |
|||
+ "&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"; |
|||
private static final String taskBegin = "title=\""; |
private static final String taskBegin = "title=\""; |
||
private static final String taskEnd = "\""; |
private static final String taskEnd = "\""; |
||
Line 622: | Line 623: | ||
private static final String exmplEnd = "</span>"; |
private static final String exmplEnd = "</span>"; |
||
private static final String editBegin = "<span class=\"editsection\">"; |
private static final String editBegin = "<span class=\"editsection\">"; |
||
/** |
/** |
||
* @param args |
* @param args |
||
Line 633: | Line 634: | ||
ScreenScrape ss = new ScreenScrape(); |
ScreenScrape ss = new ScreenScrape(); |
||
String rootPage = ss.read(rootURL); |
String rootPage = ss.read(rootURL); |
||
while(rootPage.contains(taskBegin)){ |
while (rootPage.contains(taskBegin)) { |
||
rootPage = rootPage.substring(rootPage.indexOf(taskBegin) |
rootPage = rootPage.substring(rootPage.indexOf(taskBegin) |
||
+ taskBegin.length()); |
|||
String title = rootPage.substring(0, rootPage.indexOf(taskEnd)); |
String title = rootPage.substring(0, rootPage.indexOf(taskEnd)); |
||
if (!title.contains("Category:")) { |
if (!title.contains("Category:")) { |
||
Line 642: | Line 644: | ||
} |
} |
||
// Loop through each task and print count |
// Loop through each task and print count |
||
for(String task : tasks) { |
for (String task : tasks) { |
||
String title = task.replaceAll("'","'"); |
String title = task.replaceAll("'", "'"); |
||
String taskPage = ss.read(baseURL+title.replaceAll(" ", "_")); |
String taskPage = ss.read(baseURL + title.replaceAll(" ", "_")); |
||
int exSubTot; |
int exSubTot; |
||
if (taskPage.contains(exmplBegin)) { |
if (taskPage.contains(exmplBegin)) { |
||
int startPos = taskPage.lastIndexOf(exmplBegin) |
int startPos = taskPage.lastIndexOf(exmplBegin) |
||
+ exmplBegin.length(); |
|||
String countStr = taskPage.substring(startPos, |
String countStr = taskPage.substring(startPos, |
||
⚫ | |||
taskPage.indexOf(exmplEnd, startPos)); |
|||
⚫ | |||
exSubTot = Integer |
|||
⚫ | |||
⚫ | |||
⚫ | |||
: countStr); |
|||
⚫ | |||
exSubTot = 0; |
exSubTot = 0; |
||
while(taskPage.contains(editBegin)) { |
while (taskPage.contains(editBegin)) { |
||
taskPage = taskPage.substring(taskPage |
taskPage = taskPage.substring(taskPage |
||
.indexOf(editBegin) + editBegin.length()); |
|||
exSubTot++; |
exSubTot++; |
||
} |
} |
||
} |
} |
||
exTotal += exSubTot; |
exTotal += exSubTot; |
||
System.out.println(title+": "+exSubTot+" examples."); |
System.out.println(title + ": " + exSubTot + " examples."); |
||
} |
} |
||
// Print total |
// Print total |
||
System.out.println("\nTotal: "+exTotal+" examples."); |
System.out.println("\nTotal: " + exTotal + " examples."); |
||
}catch(Exception e){ |
} catch (Exception e) { |
||
System.out.println(title); |
System.out.println(title); |
||
System.out.println(startPos+":" |
System.out.println(startPos + ":" |
||
+ taskPage.indexOf(exmplEnd, startPos)); |
|||
System.out.println(taskPage); |
System.out.println(taskPage); |
||
e.printStackTrace(System.out); |
e.printStackTrace(System.out); |
||
Line 671: | Line 679: | ||
} |
} |
||
} |
} |
||
</lang> |
</lang> |
||
[[Count programming examples/Java/ScreenScrape|ScreenScrape class]] |
[[Count programming examples/Java/ScreenScrape|ScreenScrape class]] |