PDA

View Full Version : JAVA help


aznkidlee
04-18-2004, 10:30 AM
I need help writing this program.

Write the printWord(nextWord, cursorPos, lineWidth) method that prints nextWord to System.out. cursorPos indicates the number of characters already printed on the current line. The word must fit on the line so that the total width of the line does not exceed lineWidth. If the word doesn't fit, the line is ended and the ord is printed on the next line, starting at cursor position 0. If nextWord is too long to fit on a line by itself, it is truncated. You can use the following algorithm:

1) If this is not the first word on the line (ie if cursorPos > 0) and the word does not fit on this line, call System.out.println() and reset cursorPos to 0.

2) If the word is not the first on the line, print a single space and increment cursorPos.

3) If nextWord fits on this line, print it and add its length to cursorPos; otherwise print the longest substring of nextWord that fits and set cursorPos to lineWidth.

4) Return the new cursor position.

This is what I have so far.

private int printWord(String nextWord, int cursorPos, int lineWidth)
{
int length = nextWord.length();
int index=0;

if (cursorPos > 0 && cursorPos < lineWidth)
{
System.out.print (" " + nextWord);
}

while (index < length)
{

//counts the number of characters in the string
for (char i='a'; i<='z'; i++)
{
if (nextWord.charAt(index) == i)
{
cursorPos+=1;
}
}
index+=1;

}
System.out.print (nextWord);
return cursorPos;
}