Jave method to take command line argument


Can someone advise what is wrong with this code I have a class that I want to send a URL to and it should read in the data from the URL of the webpage and return it in a string to others.

I keep getting error Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      The method openStream() is undefined for the type String


public String GetURL (String URL){
try
{
//    URL url = new URL( args[0] );
      String url=URL;
      String data="";
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;

    while ((str = in.readLine()) != null)
    {
      data=data+str;
    }
    in.close();
    return data;
enigma1234567890Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

moosely123Commented:
This error is referring to the name of the variables you have defined.

You have a URL variable name url and also a String variable name url. You should change the Sring variable name to something like urlString as i assume its a string representation for a URL.

This needs to be changed because the url part in the url.openStream() is referring to the string object where you really mean the URL object.

In future you should try to distingish more between variable names to make it easier to read your code.

So try these changes should solve it.



public String GetURL (String URL){
try 
{
//    URL url = new URL( args[0] );
      String urlString = URL;
      String data="";
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;

    while ((str = in.readLine()) != null) 
    {
      data=data+str;
    }
    in.close();
    return data;

Open in new window

0
Mick BarryJava DeveloperCommented:
>       String url=URL;

try changing that to:

      String url=new URL(URL);

http://helpdesk.objects.com.au/java/how-to-write-get-response-to-byte-array
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robthewolfCommented:
I would try this, I think moosely has made a typo
public String GetURL (String urlString){
try
{
      URL url = new URL( urlString );
      String data="";
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;

    while ((str = in.readLine()) != null)
    {
      data=data+str;
    }
    in.close();
    return data;
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

enigma1234567890Author Commented:
stuck on this neither seem to work

I want to take a command line argument in the main class and pass to a method in a different class does this help
0
Mick BarryJava DeveloperCommented:
what do you currently have?
0
robthewolfCommented:
you have only provided the code that reads the website.  What is the class that it is in?

you want to do it with two classes try this

public class Test {
  public static void main(String[] args){
    UrlReader ur = new URLReader();
    ur.getURL(args[0]);
  }

}

public class UrlReader {

 public getURL(String urlString){
try
{
      URL url = new URL( urlString );
      String data="";
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;

    while ((str = in.readLine()) != null)
    {
      data=data+str;
    }
    in.close();
    return data;
   } catch (Excetpion e){
   }
   }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.