• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mod Function with multiples?  RSS feed

 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm currently finishing an assignment where I have to write a Java program that asks for user input regarding a book and provides information based on that input like the number of pages required, number of words on the final page and if the final page is more than half full, etc. We are told that each page should have 250 words maximum to use for our calculations. The problem I am having here is that when the total number of words is a multiple of 250 the output tells me that there are 0 words on the last page when in fact there are 250. I've tried messing around with mod functions in this format ((x+a)%250)+y which is what the professor told us to use instead of creating an if-statement for this case but I haven't been able to crack it. I don't think I fully understand how the mod function cycle could work for this case, if anyone could help me that'd be great!


TL;DR Above if totalWords is a multiple of 250 like 17500 then wordsFinalPage will give us 0 and I need it to give 250. How can I do that without using an if statement but instead using a mod function?
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really sure you want this line?

double totalPages = Math.ceil(totalWords / 250.0);
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to add that line because one of the inputs required had 372 total words and without Math.ceil it would round the result of 372/250 which is 1.4 to 1 and since anything with a decimal here would require another page the result of totalPages has to be rounded to ceiling.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to get a piece of paper and write down how you are going to calculate the number of pages and words on the last page. When you have got it worked out on paper, it will be easy to turn into code.
Do show us what you get.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really using JOptionPane for input? that is very old‑fashioned style.
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I didn't reply before! JOptionPane is used because it's what the professor asked for, everything in the code is what was asked for. And I have to enter three different inputs (info about books) but the only one that's giving me a problem is the one with 17500 total words because of it being a multiple of 250.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JOptionPane still works, but it went right out of fashion when Scanner was introduced in 2004.
I think you will have to go back to pencil and paper to get a formula which gives 250 from 17500 and 250. It is similar to what you showed earlier, and I think there is quite a simple solution.
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying but I can't find a solution that won't affect the other results. Like the other input that has 372 total words will be affected if I add something to x or to the mod function for 17500. I just can't figure it out.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about subtracting 1, taking the remainder, and adding the 1 back?
 
Fran Correia
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I didn't even think of that, I was looking at it as this whole complicated thing and I didn't realize the potential of doing a calculation before and after with the parenthesis! Thank you, I finally understand how it works!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is
((x+a)%250)+y
… where a = −1 and y = 1.

And, “you're welcome”
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!