• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

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;
0
enigma1234567890
Asked:
enigma1234567890
3 Solutions
 
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
 
objectsCommented:
>       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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
objectsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now