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){}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.