0

I am pretty new to java so I understand my code will not be very good. 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.

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); Hope this makes sense.

Thanks Simon

3
  • It's not answering your question but I suggest you have a look at jaunt if you want to scrape a website for information. Commented Apr 24, 2014 at 13:25
  • Just a syntax trick: flag == false is pretty uncommon, !flag is shorter to write (and, my opinion, more readable) :) Commented Apr 24, 2014 at 13:50
  • Ill remember in the future these tips :) I guess im still used to the old VB ways lol Commented Apr 28, 2014 at 13:08

1 Answer 1

2

You can take advantage of Java collections, for example ArrayList. Just define the name as:

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

and later use it like this:

name.add(new GameInformation(gameName));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.