我想使用几个while循环从Amazon的“搜索结果”页面获取所有jpg文件实例。我已经包含了system.out.println语句来帮助我跟踪我的代码在终端输出中所做的事情。 java程序成功遍历str3字符串,直到它按顺序找到“s”“r”“c”,因此它知道源是未来的。然后接下来的42个字符,看看结果代码是否与亚马逊网站上用于显示图像的代码相同"src=http://ecx.images-amazon.com/images/I/"
。循环找到了"src=http://ecx.images-amazon.com/images/I/"
中的所有字符,并转换了将字符数组转换为名为temp的字符串。我比较temp与String变量stringToFind的比较。这两个是相同的,我检查了输出,并验证使用了.equals()而不是==。我没有最微弱的线索,为什么第二个if语句的比较不起作用。请帮忙!
编辑:TL; DR:if语句与注释的比较//不要输入循环!!!!不起作用。
import java.util.Arrays;
public class JpgFinder {
//Finds the url for a jpg file within Amazon.ca search results page //source code so that the image results of a users search may be stored.
public static void main(String[] args) {
String str1 = "src=http://ecx.images-amazon.com/images/I/31IVWofSY8L._AA160_.jpg onload=";
String str2 = "src=http://ecx.images-amazon.com/images/I/31ZTujPkvvL._AA160_.jpg onload=";
String str3 = str1 + str2;
int str3Length = str3.length();
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int sum = 0;
String temp = "";
char[] charArray = new char[100];
char[] charArray2 = new char[100];
String[] jpgArray = new String[500];
boolean jpgFound = false;
//Searches for src
while (counter1 < str3Length) {
System.out.println("1");
if ((str3.charAt(counter1) == 's') && (str3.charAt(counter1 + 1) == 'r') && (str3.charAt(counter1 + 2) == 'c')) {
//Found src
System.out.println("2");
counter3 = counter1;
while (counter2 < 42) {
//Takes src=http://ecx.images-amazon.com/images/I/
System.out.println("3");
charArray[counter2] = str3.charAt(counter2);
counter2++;
counter1++;
}
temp = new String(charArray);
String stringToFind = "src=http://ecx.images-amazon.com/images/I/";
System.out.println(temp);
System.out.println("4");
if (temp.equals(stringToFind)) {
//If src=http://ecx.images-amazon.com/images/I/ is compared and confirmed, continue
//DOES NOT ENTER LOOP!!!!
System.out.println("5");
while ((counter2 < 82) && jpgFound == false) {
if ((str2.charAt(counter2) == '.') && (str3.charAt(counter2 + 1) == 'j') && (str3.charAt(counter2 + 2) == 'p') && (str3.charAt(counter2 + 3) == 'g')) {
counter2++;
jpgFound = true;
counter4 = counter2 + 3;
sum = counter4 - counter3;
System.out.println("6");
while (counter5 < sum) {
charArray2[counter5] = str3.charAt(counter5);
System.out.println("7");
}
}
else {
counter2++;
System.out.println("8");
}
}
}
System.out.println("9");
System.out.println("DID NOT ENTER");
}
String temp2 = new String(charArray2);
jpgArray[counter6] = temp2;
counter6++;
counter1++;
System.out.println("10");
}
System.out.println("Second attempt: " + temp);
System.out.println("Jpgs: " + Arrays.toString(jpgArray));
}
}
输出:
1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 SRC = http://ecx.images-amazon.com/images/I/ 4 9 不要输入 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 2
src=http://ecx.images-amazon.com/images/I/
4
9
不要输入
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
1
10
第二次尝试:src=http://ecx.images-amazon.com/images/I/
Jpgs:[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null ,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null ,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null ,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null ,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null ,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,nul l,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
答案 0 :(得分:1)
这是因为temp和stringToFind不相等。
temp的长度为100,stringToFind的长度为42。
为什么temp的长度为100?因为new String(charArray)创建了一个String解码 all 数组中的字节。包括你没用过的那些。 charArray被分配了一个包含100个元素的数组。
此外,使用IDE调试支持会很有效,就像在Netbeans或Eclipse中一样。用打印调试程序很麻烦。
答案 1 :(得分:0)
我认为你应该使用String类的indexOf(String str)方法来获取&#34; src =&#34;的位置。字符串,然后使用substring(int,int)方法获取包含所需字符串部分的子字符串,然后可以使用equals方法。