This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Assignment 1b Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Assignment 1b" Watch "Assignment 1b" New topic

Assignment 1b

Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Suppose my console takes 80 characters, that I've printed, say, up to the 75th position, and am trying to print out a 4-character name as many times as I can on a line without the name carrying over to the next line. I've found that when I use the System.out.println command to print that name in that situation, the next line is left blank and I resume printing on the line below it.
Here's my question: is this because the S.o.pln command prints out a character *besides* the name that brings one to the next line, and that when a character is put in the last position on the line, there is an automatic carry to the next line? If not, then why on earth does this happen?
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
It's an operating system peculiarity. It's not a Java thing. However, you will want to avoid that extra carriage return/line feed in your assignment.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
i put an extra "if" statement in my first attempt to solve that problem, but it got picked by bill... it actually happens with 4, 7, 9, 19... letter names because with the extra " " they become 5, 8, 10 and 20 characters which fit neatly on one line of 80... and if you start a new line, it actually goes to the next one.
maybe i souldn't give this away yet, but think about a line that holds 79 characters instead of 80... i had a similar problem in c++ last year, but i forgot all about it.

Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1793
I had the same thing happening (Sun JDK 1.3.0_02 on Windows 2000). Any time the name + 1 space was evenly divisible by 80, I got the extra blank line.
"Gertrude " would work correctly for 80 columns, but "Kim ", "Mike ", and "Michael " would have an extra blank line.
I dealt with the problem by coding as if the console were only 79 columns wide.
[This message has been edited by Michael Matola (edited April 26, 2001).]
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Everytime the last column is filled the OS initiates a linefeed.
An overly simplistic way of looking at this is to think each time a position is filled on the screen a pointer(position reference) is moved to the next available position. If your screen is set to 80 columns (usually default) then, when the 80th position is filled the next available position is the first position on the next line (OS initiated line feed). Now if your code initiates a '\n' the pointer will move to the first position on the next line leaving a blank line. If you changed your screen to 132 column you would not encounter the OS initiated linefeed until the 132nd column had been filled. You can see this at work by simply typing and noting that the cursor (position reference) jumps forward to the next empty position automatically after each keypress. When the 80th column is filled the OS initiates a linefeed and the cursor jumps to the first position on the next line.
I'm sure there is a more technical way of explaining this, but hey this works for me.
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Thanks for the suggestion, Greg. I had thought of the 79-character move, but I've thought of something else that might work just as well without having to use that move. I don't want to spoil the challenge for others if the idea works, but consider this question: how can we get the program to see that it *would* fill the line if it printed the string some number of times, and print the string just one less time than that?
Maybe that's too obtuse, but I hope not.
I agree. Here's the link:
subject: Assignment 1b
It's not a secret anymore!