Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having issues implementing this code in a class. Does anyone have any sugge

ID: 3564633 • Letter: I

Question

I'm having issues implementing this code in a class. Does anyone have any suggestions?

As an experiment, place this code inside a function of a class and execute it. You should see the same html source print out on the console as you saw in your browser.
Assignment

URL url = new URL("http://finance.yahoo.com/q?s=msft");

HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.connect();

InputStream inputStream = connection.getInputStream();

Scanner input = new Scanner(inputStream);

String content = new String();

while (input.hasNextLine()) {

content += " " + input.nextLine();

}

System.out.println(content);

Explanation / Answer

If you're just trying to run their code, you just need to import the necessary packages and catch the exceptions. This code runs fine:

import java.net.*;
import java.io.*;
import java.util.*;
import java.net.MalformedURLException;

public class test {
   public static void main(String[] args) {

       try{
       URL url = new URL("http://finance.yahoo.com/q?s=msft");

       HttpURLConnection connection = (HttpURLConnection)url.openConnection();

       connection.connect();

       InputStream inputStream = connection.getInputStream();

       Scanner input = new Scanner(inputStream);

       String content = new String();

       while (input.hasNextLine()) {

             content += " " + input.nextLine();

       }

       System.out.println(content);
       }
       catch(MalformedURLException e){}
       catch(IOException e){}
  
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote