• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java-1b

 
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic