Forums Register Login

Assignment 1b

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 843 times.
Similar Threads
for loops
Why RegConnectRegistry works in my exe but not via JNI?
line continuation characters
1.4 (Say)
Printing new line in HTML
Question about Cattle Drive practice assignment
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:32:00.