我对java很新,所以我理解我的代码不会很好。但是我正在尝试使用while循环创建一个新的类对象,它将1,2,3等等作为变量,然后在从网站中提取游戏后会有游戏名称。
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class GameInformationPuller
{
public static void PullInformation() {
String gameName = "";
String gameRating = "";
String gameGenre = "";
int count = 0;
int i = 0;
StringBuilder sb = new StringBuilder();
GameInformation name;
boolean flag;
try {
URL url = new URL("http://www.mmorpg.com/gamelist.cfm/show/all/sCol/titleUC/sOrder/asc");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\data4.txt"));
String line;
while ((line = reader.readLine()) != null)
{
if(line.contains("http://www.mmorpg.com/gamelist.cfm/game/") || line.contains("<span class=\"value rating\">") || line.contains("title=\"Not enough votes to tabulate\">") || line.contains("<td class=\"genre\">"))
{
if(!line.contains("Jump to Random Game"))
{
sb.delete(0, sb.length());
//------------------------------------------------
if(line.contains("http://www.mmorpg.com/gamelist.cfm/game/"))
{
flag = false;
count = 0;
i = 0;
while(i < line.length() - 1 && flag == false)
{
if(count >= 2 && flag == false)
{
if(line.charAt(i) == '<')
{
flag = true;
}
else
{
if(!(line.charAt(i) == '.'))
{
sb.append(line.charAt(i));
gameName = new String(sb);
}
}
}
if(line.charAt(i) == '>')
{
count++;
}
i++;
}
}
//------------------------------------------------
name = new GameInformation(gameName);
}
//------------------------------------------------
}
else
{
//Nothing
}
}
}
//Close reader and writer
reader.close();
writer.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
我希望在此部分找到的每个游戏名称都有一个新变量: name = new GameInformation(gameName); 希望这是有道理的。
由于 西蒙
答案 0 :(得分:2)
您可以利用Java集合,例如ArrayList
。只需将name
定义为:
ArrayList<GameInformation> name; //or names makes more sense
以后使用它:
name.add(new GameInformation(gameName));