2014-04-24 1 views
0

나는 자바에 대해 매우 익숙하므로 코드가 좋지 않을 것이라는 것을 이해합니다. 그러나 나는 새로운 클래스 개체를 만들려고 while 루프를 사용하여 같은 1,2,3 등 변수로 누른 다음 그것은 게임 이름을 웹 사이트에서 당겨 후 것입니다. while 루프에서 다른 변수를 사용하여 클래스 객체 만들기

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(); 
    } 
} 

} 

내가 그것을이 부분에서 발견 각 게임 이름에 대한 새로운 변수합니다 : 이름을 = 새로운 GameInformation (gameName); 희망적이라고 생각합니다.

감사 사이먼

+0

: // 소풍-API를 .com /) 웹 사이트를 긁어 정보를 얻으려면. –

+0

그냥 구문 트릭입니다 :'flag == false'는 꽤 흔하지 않습니다.'! flag'는 글자를 쓰는 것이 더 짧습니다. (그리고 제 생각에는 더 읽기 쉽습니다) :) – NiziL

+0

미래에이 팁들을 기억하십시오 :) 나는 아직도 사용하는 것 같습니다. 이전 VB 방식으로 LOL – user3568925

답변

2

당신은 예를 ArrayList를 들어, 자바 컬렉션을 이용할 수 있습니다. 그냥 정의 name로 :

ArrayList<GameInformation> name; //or names makes more sense 

나중에 같이 사용 : 그것은 당신의 질문에 대답 아니에요하지만 난 당신이 [소풍] (HTTP 한 번 봐 가지고 제안

name.add(new GameInformation(gameName)); 
+0

아픈 도움을 주셔서 감사합니다. – user3568925

관련 문제