0

I'm getting really confused about this issue and need clarification as soon as anyone can give some.... I apologize for formatting. Right after I try to open the connection, the program seems to simply halt.

public void run() {
        try {
            System.out.println("Started to run");

            //The first thing we do is connect with the website and instantiate a Scanner for the data.
            URL address = new URL("http://www.example.com");
            URLConnection connection = address.openConnection();
            Scanner scan = new Scanner(connection.getInputStream());
            System.out.println("Established connection");

            //We lop off the first 10 blank lines that we'll scan, plus the <body> tag. 
            for (int i = 0; i <= 10; i++) {
                scan.nextLine();
            }

            while (scan.hasNextLine()) {
                synchronized(lock) {//SYNCHRONIZE

                    //First, we check if we've reached the end.
                    String temp = scan.next(); 
                    if (temp.equals("</body>")) {
                        break; //If we have, cease processing.
                    }
                    year = Integer.parseInt(temp); 
                    boyName = scan.next(); boyPrevalence = scan.nextInt(); girlName = scan.next(); 
                    //(Handling the <br> HTML code.)
                    String next = scan.next();
                    next = next.replace("<br>", ""); 
                    girlPrevalence = Integer.parseInt(next);

                    //Now we update our almanac of boys' names for the given year.
                    if(boyNameMap.containsKey(year)) {
                        boyNameMap.get(year).put(boyName, boyPrevalence);
                    } else {
                        Map<String, Integer> tempmap = new HashMap<>();
                        tempmap.put(boyName, boyPrevalence);
                        boyNameMap.put(year, tempmap);
                    }

                    //And now we update our almanac of girls' names.
                    if(girlNameMap.containsKey(year)) {
                        girlNameMap.get(year).put(girlName, girlPrevalence);
                    } else {
                        Map<String, Integer> tempmap = new HashMap<>();
                        tempmap.put(girlName, girlPrevalence);
                        girlNameMap.put(year, tempmap);
                    }

                    System.out.println(year + "; " + boyName + "; " + boyPrevalence + "; "
                            + girlName + "; " + girlPrevalence);

                }//DESYNCHRONIZE
            }
            scan.close();
        }
        catch (IOException e) {
            System.out.println("File not found.");
        }

That is, "Established connection" never prints.

11
  • Did you try to debug? If open connection get problem then after a few second try to connection program will throw exception. Commented May 13, 2017 at 3:17
  • The print statement was me trying to debug, I didn't get any info. The program didn't even make it to the print statement. Commented May 13, 2017 at 3:18
  • Have you tried using a debugger? Commented May 13, 2017 at 3:20
  • Don't understand, what do you mean? Commented May 13, 2017 at 3:21
  • Do you mean you don't know what a debugger is? Commented May 13, 2017 at 3:21

1 Answer 1

1

You can see the code I am trying and the result enter image description here

Sign up to request clarification or add additional context in comments.

11 Comments

Well Jesus, that's bizarre. I dunno what my problem is then :/
Did you try to check if your run method is called? If yes let's try shorten your code like mine to test it. Problem still the same then try to restart your IDE...
My output from elsewhere says that a thread was run, so run() did get called as far as I know.
Again .... my suggestion to use a debugger! You need to be sure what is happening here, rather than relying on guesswork / intuition.
If your run method is called then you must see the output: Started to run did you see it? Don't guest! Let's try to debug your program
|

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.