aspose file tools*
The moose likes Cattle Drive and the fly likes Java-1b: Pretend the line-width is 79 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Java-1b: Pretend the line-width is 79" Watch "Java-1b: Pretend the line-width is 79" New topic
Author

Java-1b: Pretend the line-width is 79

ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
Currently, my program has the following logic

1. Make a String variable "name", which consists of the input argument and a white-space (" ")
2. Use the line-width and length of "name" to find out the maximum number of entries ("wordPerLine") per line (entry being the input arg and " ")
3. Use a for loop
4. If the i-th entry is a multiple of "wordPerLine", only print the input argument but not the white-space (" "), to avoid printing 2 new-lines in some OS
5. Else, print out the varialbe "name"

My method works but the suggestion from the teachers are to "pretend the line-width is 79, not juggling with the variable "name""

For the life of me, I couldn't figure this out. Can someone please give me a hint? Thank you.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Remember that your nitpickers are looking for simple, easy to read code.
While your method works, it may be unecessarily complex.
A clue to simplifying your code is being suggested by pretending the line length is 79 characters long.
Given this clue, how can you write simpler, more easy to read code?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Sometimes you add the space and sometimes you don't. I think it would be simpler to always add the space, thus removing the need for an if statement.


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

Joined: May 04, 2008
Posts: 32
Let's take the input "Marilyn " as an example.

If I always print the white-space, then the 80th column will be filled. Since I have to have a new-line after the last word is printed (not all OS will give me a new line automatically), I thought the if statement is necessary.

Now, if I use line-width as 79, only 9 "Marilyn " will be printed per line. Am I missing something? Thank you.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
A clue:
It is not a requirement of the assignment that each line be filled exactly to the 80th slot if possible. Does that help somewhat?
ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
I understand that. Your point is, unfortunately, also my source of confusion.

Back to the "Marilyn " example. Say the line-width is 79, then what I want is,

9 "Marilyn " and 1 "Marilyn" (9 * 8 + 7 = 79)

For the 10th "Marilyn" to fit in the first line and a line-width of 79, I have to take out the space after the 10th "Marilyn". Without an if statement checking for every 10th "Marilyn" and take out the corresponding white-space, I will always fill up all 80 slots.

And just to clarify, I do want 10 "Marilyn" in 1 line, right?

Thanks in advance for your help.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
For reasons of aesthetics and everything fitting just perfectly, I, too wanted to use the entire space available if the name fit. However, for the purposes of this assignment, it is not required.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
You don't need Marilyn ten times in the line. You want the simplest code that will work whether the OS gives you a new line automatically or not. You said, "If I always print the white-space, then the 80th column will be filled." That is correct. If the 80th column is filled, sometimes you will get a new line and sometimes you won't. So I suggest that you should avoid the 80th column totally and avoid the problem. That is a much simpler solution than an if statement. How can you avoid the 80th column? By pretending that you only have 79 columns available.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java-1b: Pretend the line-width is 79