• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment 1b

 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9062
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic