Can someone explain this code to me

Discussion in 'Java' started by pandu345@yahoo.com, Oct 6, 2006.

  1. pandu345@yahoo.com

    pandu345@yahoo.com

    Joined:
    Oct 6, 2006
    Messages:
    1
    Hi,

    //I am initializinng the URL object
    URL addr = new URL("http://sfbay.craigslist.org/rid/");

    //Opening a bufferedinputstream object
    BufferedInputStream bin = new BufferedInputStream(addr.openStream());

    //Initialize a Stringbuffer
    StringBuffer bfr = new StringBuffer();

    //Declare a byte array
    byte[] b = new byte[1024];

    ???????? I dont understand what these following two lines do
    for (int n; (n = bin.read(b)) != -1;) {
    bfr.append(new String(b, 0, n));

    //This returns the string value the StringBuffer
    bfr.toString()

    Can someone explain this.

    Thanks,
    pandu
     
    pandu345@yahoo.com, Oct 6, 2006
    #1
    1. Advertising

  2. pandu345@yahoo.com

    mosborn

    Joined:
    Sep 20, 2006
    Messages:
    7
    Location:
    Bowling Green, OH, USA
    Pandu,

    This uses the for loop's iteration mechanism to read from the BufferedInputStream into the byte array. Thanks to the parentheses and order of operations, bytes are read into b, the return value for read() (the number of bytes read) is assigned to n, then the value of n is compared to -1 to decide whether to break out of the loop.

    The second line, which obviously creates a new String from the byte array and appends it to the StringBuffer, uses the more elaborate String constructor because the read operation might not fill up the entire 1024 bytes in the array.

    A small improvement on this method (taken as it is) might be to avoid creating Strings in the for loop. Instead, use a ByteArrayOutputStream to receive the byte array contents, and after all the data is read, simply call ByteArrayOutputStream.toString(). Should be more efficient.

    Matthew
     
    mosborn, Oct 12, 2006
    #2
    1. Advertising

  3. pandu345@yahoo.com

    ACE01

    Joined:
    Nov 7, 2006
    Messages:
    2
    hi can someone help me with this code

    hi i need help on cracking this java code i am not good at java i need some1 to help me the code is

    /**
    * Routine to calculate where a pair of lines intersect (if they do).
    *
    * @param (integers) X co-ordinates and Y co-ordinates of both ends of two lines.
    * output: matrix size 3 to hold (X,Y) of intersection,
    * and integer to indicate which type of intersection
    * 3 = true intersection
    * 2 = line segment 2 only
    * 1 = line segment 1 only
    * 0 = not on either segment
    * -1 = parallel
    * -2 = colinear
    */
    public int [] lineIntersection(int x1Coord1, int y1Coord1,
    int x1Coord2, int y1Coord2,
    int x2Coord1, int y2Coord1,
    int x2Coord2, int y2Coord2) {
    int result [];
    result = new int [3];

    // replace the following three lines of code with the correct code
    result[0]=0; // X co-ordinate
    result[1]=0;// Y co-ordinate
    result[2]=0; // intersction type

    return result;
    } // end lineIntersection()


    /**
    * lineEquation routine to calculate the equation
    * of a straight line from the co-ordinates
    * of two points on that line.
    * The equation is of the form AX + BY + C = 0
    *
    * @param X and Y co-ordinates of two points on line
    *
    * output: matrix size 3 to hold coefficients of line equation (A, B, and C)
    *
    */

    public int [] lineEquation(int xCoord1, int yCoord1, int xCoord2, int yCoord2) {
    int [] result;
    result = new int [3]; // to hold values of A, B, and C

    // replace the following three lines of code
    // with the correct code

    result[0] = 1;
    result[1] = 1;
    result[2] = 1;

    return result;
    } // end lineEquation()
     
    ACE01, Nov 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suzy
    Replies:
    3
    Views:
    466
  2. Aarti
    Replies:
    3
    Views:
    286
  3. Angus
    Replies:
    3
    Views:
    314
    Andrey Tarasevich
    Jun 23, 2008
  4. Soumen banerjee

    Can someone please explain to me this code?

    Soumen banerjee, May 3, 2009, in forum: Python
    Replies:
    3
    Views:
    417
  5. effendi@epitome.com.sg

    Can someone explain this code line to me?

    effendi@epitome.com.sg, Apr 3, 2005, in forum: Javascript
    Replies:
    1
    Views:
    92
    Evertjan.
    Apr 3, 2005
Loading...

Share This Page