代码应该读取包含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);
}
}
}
答案 0 :(得分:0)
您可以使用正则表达式提取文本中的所有数字。在此之后你需要做的就是找到最大值。
public static List<Double> findNumbers(String bigText) {
List<Double> numbers = new ArrayList<Double>();
Pattern p = Pattern.compile("[0-9]+.[0-9]*|[0-9]*.[0-9]+|[0-9]+");
Matcher m = p.matcher(bigText);
while (m.find()) {
numbers.add(Double.parseDouble(m.group());
}
return numbers;
}
然后你可以找到最容易的:
public double findMaximum(List<Double> numbers) {
double max = 0.0;
for(double d: numbers) {
if(d > max) max = d;
}
return max;
}