Can someone explain this code to me

Joined
Oct 6, 2006
Messages
1
Reaction score
0
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
 
Joined
Sep 20, 2006
Messages
7
Reaction score
0
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
 
Joined
Nov 7, 2006
Messages
2
Reaction score
0
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()
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top