javaCoderMakeSimple
javaCoderMakeSimple

Reputation: 69

why i am not able to read Html Content from a website in a file?

I have made a java program where in i can use any website to read its Html Content using Scanner class and Varargs.I am not able to get the output while i am using Scanner class and VarArgs.

Below is the following Code.

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;

public class ReadWebsite 
{
 public static void main(String[] args) throws Exception
 {

    URL oracle = new URL(args[0]); 
    Scanner s=new Scanner(oracle.openStream()); 

    while (s.hasNext())
    {
        System.out.println(s.nextLine());
    }
  s.close();
 }
}

OutputShown
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at oodlesTech.ReadWebsite.main(ReadWebsite.java:15)

Upvotes: 0

Views: 53

Answers (2)

KDP
KDP

Reputation: 1481

if you are running from eclipse you have to pass the arguments.

right click program - > run as - > run configurations -> arguments ->program arguments . in this tab pass the actual url which will be passed as args[0] to your main method.

Upvotes: 1

TheCodingFrog
TheCodingFrog

Reputation: 3514

You are not passing the argument to your java program. For your testing you can either hard code it in code e.g. URL oracle = new URL("http://www.google.com"); or pass an argument to your java program, explained here

Upvotes: 1

Related Questions