Results 1 to 15 of 15
  1. #1
    abravo227 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Helping a rookie with coding!

    Hey guys,

    First off I would like to say hello, I am new to the boards and plan on visiting quite often as it seems there is a lot of help around here! Anyways, I was curious if somebody could help me out. I am working on my last assignment in a java course but cannot seem to figure out this part of the coding.

    openInput is a method that takes in a String that is the url address to an input file somewhere on the web. Then, you must use that String to create a new URL object, and create a URLConnection object by opening a connection to the URL object. Finally, get an InputStream from the URLConnection object, and use it to set the value of iStream. All exceptions must be caught.

    I can make the method but what throws me off is tying in the URL address. Can anyone offer me a rough draft of what this coding would look like? Thanks in advance for the help!

  2. #2
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    I aren't getting the question very clearly.
    Do you mean something like, accessing a file from your web browser?

  3. #3
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    Have you looked at the constructors for the URL class?

    The java doc can be found here. And a tutorial here.

  4. #4
    abravo227 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    I am having the same trouble understanding the assignment. Here is the other information he gave me to use. Maybe it will help you guys more than me haha...

    Within the first class I am supposed to include these rules -"iStream is an instance variable that is an InputStream object."
    "openInput is a method that takes in a String that is the url address to an input file somewhere on the web. Then, you must use that String to create a new URL object, and create a URLConnection object by opening a connection to the URL object. Finally, get an InputStream from the URLConnection object, and use it to set the value of iStream. All exceptions must be caught."
    The final rule for the class being:
    "download is a method that takes in a String that is the directory path/filename of the local file where the information from the web should be written. This method should use a Scanner object to read information from the InputStream iStream and copy it line-by-line into the local file using a FileWriter object. This method must return true if the copy was successful, and return false if the copy was unsuccessful (i.e., file does not exist). All exceptions must be caught."

    I then create a test drive class with a webReader object and a scanner.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Helping a rookie with coding!

    It's hard to know exactly where you're stuck without code. Can you show us your latest code attempt and any errors that you may be getting?

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    abravo227,

    I get the distinct feeling you have yet to put any effort into your own assignment, in the hopes that someone here will post a solution. If that is the case, you will be very disappointed. Our goal is to help you learn, not do the work for you.

    "iStream is an instance variable that is an InputStream object." - You know how to define an instance variable right?
    "openInput is a method that takes in a String that is the url address..." - You know how to define a method right? You were told what to name it and what its input will be and what it should do.

    If you don't know how to define a class with instance variables and methods, please say so and we can link you to tutorials that can help teach you. Otherwise, you should only need the links in my original reply to gain enough information to complete what the assignment requires.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Helping a rookie with coding!

    At the very least, if you don't have code, then post a detailed list of exactly what you *do* know you're supposed to do for this assignment and an even more detailed list of exactly what you *don't* know how to do. The greater the detail, and the more thinking and effort you put into creating these lists, the better your chance of getting decent detailed help.

  8. #8
    abravo227 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    Thank you guys for all of the help so far. I am not at all trying to get someone to do it for me. I am simply looking for help. I have been trying to google for help but am not finding much. I dont believe I am searching for the right thing so I came to this forum. So far I have this for code....

    Java Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class WebReader_A7part2 {
    	
    	InputStream iStream;
    	
    	Protected void openInput(string urlAddress)
    The point of the assignment is to learn remote file reading and writing as well as exceptions. Please keep the help coming! its greatly appreciated!

    I guess what im stuck on is how to take in the url address to an input file somewhere on the web.
    Last edited by Fubarable; 04-22-2012 at 10:28 PM. Reason: [B] bold tags removed and [code] code tags added

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Helping a rookie with coding!

    Now the first step is fleshing out the openInput method:

    Java Code:
    private InputStream openInput(String urlAddress) {
    
       // you've been given what steps you need to do in here:
    
       // 1) create URL object from the String parameter
       // 2) create URLConnection from the URL object
       // 3) create InputStream from URLConnection
       
       // then possibly return the InputStream. I'm not 100% sure on this bit.
    
    }
    So try to flesh these steps out using the API for these classes.

  10. #10
    abravo227 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    Java Code:
    URL myURL = new URL("http://example.com/");
    Fubarable, I found this example and am sure this is what I need for the first step but how do I create it from the string parameter? I apologize for all of the questions but truly appreciate your patience and help.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Helping a rookie with coding!

    Quote Originally Posted by abravo227 View Post
    Java Code:
    URL myURL = new URL("http://example.com/");
    Fubarable, I found this example and am sure this is what I need for the first step but how do I create it from the string parameter? I apologize for all of the questions but truly appreciate your patience and help.
    Do you mean that you understand
    Java Code:
    URL myURL = new URL("http://example.com/");
    but can't extrapolate this to
    Java Code:
    private InputStream openInput(String urlAddress) {
     
       // you've been given what steps you need to do in here:
        
       // 1) create URL object from the String parameter
       URL myURL = new URL(urlAddress); 
    
       // 2) create URLConnection from the URL object
       // 3) create InputStream from URLConnection
        
       // then possibly return the InputStream. I'm not 100% sure on this bit.
     
    }
    This is a very basic concept that you really need to understand well if you are going to progress on this project and in your course. You'd better keep studying (as should we all).
    Last edited by Fubarable; 04-22-2012 at 11:17 PM.

  12. #12
    abravo227 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    I know, I still have a lot to learn. This is a beginner course and just a little over my head right now but you are really helping. So far in my coding I have this

    Java Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class ajb09fWebReader_A7part2 {
    	
    	InputStream iStream;
    	
    	Private void openInput(String urlAddress)
    
    	    URL myURL = new URL(urlAddress);
    	    URLConnection myURLConnection = myURL.openConnection();
    	    myURLConnection.connect();
    	    ObjectInputStream ois = new ObjectInputStream(myURL);
    	} 
    	
    	boolean download(String filePath)
    	Scanner sc = new Scanner(iStream);
    Can anyone help me with this part? I have the scanner but am not sure how to have it copy my entire iStream file line by line using a FileWriter object.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Helping a rookie with coding!

    You're forgetting to use your URLConnection object held by the mURLConnection variable. This is where you get your InputStream. Again, please read the API for URLConnection to see what it can do. I have no idea what you're trying to do with that ObjectInputStream; do you?

  14. #14
    abravo227 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Helping a rookie with coding!

    I am trying to get an InputStream from the URLConnection object, and use it to set the value of iStream.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Helping a rookie with coding!

    Quote Originally Posted by abravo227 View Post
    I am trying to get an InputStream from the URLConnection object, and use it to set the value of iStream.
    Good because that's what you're supposed to be doing, and this will solve it for you.

Similar Threads

  1. Anyone feel like helping me out some more?
    By CGHMN in forum New To Java
    Replies: 20
    Last Post: 06-28-2011, 12:38 AM
  2. Helping on Calculating Pie
    By bubbamcgoo in forum New To Java
    Replies: 10
    Last Post: 02-22-2011, 02:23 AM
  3. No one is helping
    By anithajerome in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-06-2010, 12:52 PM
  4. Replies: 4
    Last Post: 10-25-2010, 03:49 PM
  5. New rookie
    By JAdeline in forum Introductions
    Replies: 2
    Last Post: 08-19-2008, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •