Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java-1b  RSS feed

 
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am struggling to think up a solution to get this one cracked and would appreciate some pointers.

I have a for loop that iterates through the number of times I want to print to the screen. Inside the for loop is an if statement that adds one to the value of i and checks whether the result modulo another number is zero. I am informed that it might be possible to remove a plus or minus one from inside a loop. And that is what I am struggling with. However I try to structure my if statement, it then falls flat on it's backside because I start the loop at 0 rather than 1.

Is this a bum steer, or is it actually possible in this instance? If the latter (and I'm inclined to believe it is) I'd appreciate some help in working through the logic.
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well first you need to find out the length of the input. Then as this output to console, you check against the console length (like 80).

The if statement inside the for loop really is the condition whether a new line is in order.

I don't think you need modulo. Yet there are many ways to do it.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had that part down, I just thought I could check if the modulus of how far through the loop I was and number of words that would fit onto a line of 80 characters was zero then I would be sorted and could print a new line each time this condition was true. But alas, with zero based numbering it's not quite so easy. Zero divided by anything is zero, and throwing in an extra condition checking for not zero results in an extra word hanging off the end of line one. Which is why I wanted to add one to the number of iterations in the conditional. Back to the drawing board - when you have a working solution in your head it's very hard to try and find another solution that does the same thing!
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just looked at the example output again. I was on the right track, I just had the order the wrong way around. Now that's picky! Let's hope that submission fairs better than the first...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!