Can someone explain this code to me

Discussion in 'Java' started by [email protected], Oct 6, 2006.


    [email protected]

    Oct 6, 2006
    Likes Received:

    //I am initializinng the URL object
    URL addr = new URL("");

    //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 = != -1;) {
    bfr.append(new String(b, 0, n));

    //This returns the string value the StringBuffer

    Can someone explain this.

    [email protected], Oct 6, 2006
    1. Advertisements



    Sep 20, 2006
    Likes Received:
    Bowling Green, OH, USA

    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.

    mosborn, Oct 12, 2006
    1. Advertisements



    Nov 7, 2006
    Likes Received:
    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.