IT Community - Software Programming, Web Development and Technical Support

get a URL from cmd line input.

This is a discussion on get a URL from cmd line input. within the Java Programming forums, part of the Software Development category; I am trying to get a URL from cmd line input...connect to that URL, then retrieve the webpage info ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-12-2007, 12:29 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default get a URL from cmd line input.

I am trying to get a URL from cmd line input...connect to that URL, then retrieve the webpage info into a file.

So far I have about 4 things (i can see) wrong with my code:

1. line 30 I need to //convert the String to a URL using toUrl() somehow. Using toURL() is not mandatory but I thought that is how I should go?? Is there an easier way?

2. at line 37 I am getting an eclipse error for the method openConnection() is undefined for type URL. I'm not sure if thats because I havent done part 1 yet or not?

3. At line 51 I am trying to write the html that is retrieved from the URL into a String format so that I can just put the String into an external file?? any feedback on that would be handy.

4. Line 59 i am trying to out.write(html) and i get an eclipse error of html cannot be resolved.

I am a beginner coder and this is obviously an intermediate assignment. But, I am trying my best. A quick answer would be excellent. Anything abstract will be totally confusing at this point. I've worked on this problem a long time now. Please help.

Code:
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;  // needed for BufferedReader, InputStreamReader, etc.
import java.net.*;
 
 
public class URL {
	 // Create a single shared BufferedReader for keyboard input
	
	private static BufferedReader stdin = 
        new BufferedReader( new InputStreamReader( System.in ) );
 
	public static void main(String[] args)throws IOException {
	
	// Prompt the user
    System.out.print( "Please type a URL with Proxy (i.e. http://www.sitename.com " );
 
    // Read a line of text from the user.
    String input = stdin.readLine();
    
    //convert String to URL using toUrl() somehow
    
    //connect to URL
    try {
    	URL url;
        StringBuilder html = new StringBuilder();
    	HttpURLConnection c = (HttpURLConnection)url.openConnection();
	    BufferedInputStream in = new BufferedInputStream(c.getInputStream());
	    Reader r = new InputStreamReader(in);	
 
	    int i;
	    while ((i = r.read()) != -1) {
	    	html.append((char) i);
	    }
	    
	    html.trimToSize();
	    /**
		 * Returns the html of this page as a String.
		 * @return The html
		 */
		public String getHtml() {
			return html.substring(0);
		}
		
	    try{
	        // Create file
	        FileWriter fstream = new FileWriter("out.txt");
	            BufferedWriter out = new BufferedWriter(fstream);
	        out.write(html);//write string html to file
	        //Close the output stream
	        out.close();
	        }catch (Exception e){//Catch exception if any
	          System.err.println("Error: " + e.getMessage());
	        } 
	        
	}// end main method
 
}//end class
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-13-2007, 01:06 AM
latchu latchu is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 21
latchu is on a distinguished road
Default Re: get a URL from cmd line input.

The most obvious problem you have is that you've tried to define a new method inside the program's main() method. This is the getHTML method that is floating randomly in the middle of the program code. Move this OUTSIDE of the main method, but not outside of the entire class obviously.

Secondly, you don't need to use the toURL() method at all. In fact the String class doesn't contain such a method. The URL class (the one in the java.net package not the one you've written) has a constructor that takes a String so you can use that as the easiest way to create a URL instance from a String. e.g. URL myURL=new URL("www.whatever.com");

The StringBuilder that you are using to store the text from the URL connection has a toString() method in it that returns it's entire contents as a String, so you don't need to call the substring() method at all.

And yeh, you need to do part one first since without the URL instance you won't be able to connect to it. I'd start with the Javadocs for the classes like URL, and String, and also maybe read the Java Tutorials a bit since they offer good advice. If your course has lab demonstrators make use of them too, since they are often more experienced in Java.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-21-2007, 11:11 PM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Exclamation Re: get a URL from cmd line input.

I am assuming that out.write(html); is what your talking about that "has a toString() method in it that returns it's entire contents as a String" although I stil get an error of "the method write(int) in the type BufferedWriter is not applicable for the arguments StringBuilder. This makes me think that "html" is still an integer and not converted to a string.

My original question of "to use the toURL() method" was not clear i guess or I'm not understanding your explination. I NEED to get from a string to a URL....but i dont have the STRING of the website until it is typed into the field before. Once the string is typed in...i need to take that string and convert to a URL.
OK
So you said.."The URL class (the one in the java.net package not the one you've written) has a constructor" and you gave an example "URL myURL=new URL("www.whatever.com");" but when i substitute my variable of "input" (which should contain the "website string") into the program i get an error of "The constructor URL(string) is undefined."

I'll ignore the error of "the method openconnection() is undefined for type URL until I can figure out how to read the URL in the previous part.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-21-2007, 11:23 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Question Re: get a URL from cmd line input.

Hi aman,

can you show your code now?
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-21-2007, 11:51 PM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Exclamation Re: get a URL from cmd line input.

sure...

Code:
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;  // needed for BufferedReader, InputStreamReader, etc.
import java.net.*;
 
 
public class URL {
	 
 
	// Create a single shared BufferedReader for keyboard input
	
	private static BufferedReader stdin = 
        new BufferedReader( new InputStreamReader( System.in ) );
 
	public static void main(String[] args)throws IOException {
	
	// Prompt the user
    System.out.print( "Please type a URL with Proxy (i.e. http://www.sitename.com " );
 
    // Read a line of text from the user.
    String input = stdin.readLine();
    
    //convert String to URL
    
    URL myURL=new URL(input);
    
    //connect to URL
 
    	URL url;
        StringBuilder html = new StringBuilder();
    	HttpURLConnection c = (HttpURLConnection)url.openConnection();
	    BufferedInputStream in = new BufferedInputStream(c.getInputStream());
	    Reader r = new InputStreamReader(in);	
 
	    int i;
	    while ((i = r.read()) != -1) {
	    	html.append((char) i);
	    }
	    html.trimToSize();
	   
	    try{
	        // Create file
	        FileWriter fstream = new FileWriter("out.txt");
	            BufferedWriter out = new BufferedWriter(fstream);
	        out.write(html);//write string html to file
	        //Close the output stream
	        out.close();
	        }catch (Exception e){//Catch exception if any
	          System.err.println("Error: " + e.getMessage());
	        } 
 
	}// end main method
}//end class
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-20-2007, 02:12 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: get a URL from cmd line input.

This is a bit of a borderline misleading error the IDE gives you. What it perhaps should say is: "Cannot find a method write(StringBuilder) in class Writer", which is the underlying cause.

What Fred likely meant is that StringBuilder has a method called toString() which--unsurprisingly--returns a String. If you check the StringBuilder documentation, you'll see that it returns "... the character sequence currently represented by this object." But you still need to call toString() to get your String object; a StringBuilder is not a String and will not be accepted as a substitute.

This is a namespace conflict. What you have is a class named URL. Your own class is your closest namespace, so any reference to URL is going to be to your class, not java.net.URL . There are two ways arounds this:

1) rename your class;

2) specify the other URL class by prefixing "java.net." so that you get "java.net.URL". You will have to repeat this at each instance.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-20-2007, 02:27 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Red face Re: get a URL from cmd line input.

Oh..thanks!


I was able to get the info I needed. Thanks again!
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templates for Input/Output in J2ME? dchips13 J2ME 3 09-30-2008 04:53 AM
Error: command line error MIDL1001 : cannot open input file wincodec.idl midl Mramesh C# Programming 0 02-18-2008 11:07 PM
How to read text area data line by line ? KiruthikaSambandam ASP and ASP.NET Programming 3 01-23-2008 09:25 PM
Off-line Marketing for Your On-line Business montyauto eCommerce 1 07-13-2007 11:09 PM
Java:Tutorial - User Input pranky Java Programming 0 02-24-2007 12:52 AM


All times are GMT -7. The time now is 10:49 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0