[英]Creating a class object with a different variable in a while loop
I am pretty new to java so I understand my code will not be very good. 我是java的新手,所以我理解我的代码不会很好。 However I am trying to create a new class object using a while loop which will have the like 1,2,3 etc as a variable and then in that it will have the games name after pulling it from a website. 但是我试图使用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();
}
}
}
I want a new variable for each game name it finds at this part: name = new GameInformation(gameName); 我希望在这个部分找到的每个游戏名称都有一个新变量:name = new GameInformation(gameName); Hope this makes sense. 希望这是有道理的。
Thanks Simon 谢谢西蒙
You can take advantage of Java collections, for example ArrayList
. 您可以利用Java集合,例如ArrayList
。 Just define the name
as: 只需将name
定义为:
ArrayList<GameInformation> name; //or names makes more sense
and later use it like this: 然后像这样使用它:
name.add(new GameInformation(gameName));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.