【发布时间】:2017-09-25 16:15:56
【问题描述】:
代码应该读取一个包含html代码的.txt文件,然后在提供的代码之间找到数字并打印出三段代码中最大的数字。大数应该是 42.25。提前致谢。
// 1.) <span class="green">24.52 ▲ 0.96%</span></td>
// 2.) <span class="green">0.68 ▲ 0.41%</span></td>
// 3.) <span class="green">42.25 ▲ 0.36%</span></td>
// need to print out 42.25 as the largest number of the three above
public class LargestNumber {
public static void name() {
// this is the pattern before the name
String patternBeforeName = "<td><a href=\"http://www.nasdaq.com/aspx/infoquotes.aspx?symbol=";
// this is the pattern after the name
String patternAfterName = "&selected=";
String bigText = IOMaster.readTextFile("Project1.txt");
String patternBeforeValuePosative = "<td><span class=\"green\">";
int indexOfBeginning = bigText.indexOf(patternBeforeValuePosative);
while (indexOfBeginning >= 0) { // check if found)
int indexOfEnd = bigText.indexOf(patternAfterName, indexOfBeginning); // start looking from the indexOfBeginning
if (indexOfEnd >= 0) { // check if found
String index = bigText.substring(indexOfBeginning + patternBeforeName.length(), indexOfEnd);
// splits the text between the html tags </a></d>
String[] indexList = index.split("</a></td>");
// for statement
for (int i = 0; i < indexList.length; i++) {
index = indexList[i];
// currently prints out 24.52
System.out.println("The name is:\t\t" + index);
}
}
bigText = bigText.substring(indexOfEnd);
indexOfBeginning = bigText.indexOf(patternBeforeName);
}
}
}
【问题讨论】:
-
这个返校季是一年中我们 Stack Overflow 想要提醒您我们不在这里为人们做作业的时候。 An open letter to students with homework problems
-
你试过调试代码吗?