# Java help (NOT JAVASCRIPT)

Watch
Announcements
#1
I'm being tasked with coding a program that prompts the user to input a number of months and it will then output the number of years and months. This is the code so far:

import java.util.Scanner;
public class Exercise2
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int allMonths, years, partMonths;

System.out.print("Age in months: ");

allMonths = sc.nextInt()

//Here i want to use an if statement to calculate years from allMonths but I'm unsure how to do it exactly
if (allMonths >= 12) {
years == 1; //This gives me an error
}else{
partMonths == allMonths; //Still unsure what to write for this part
}

//Code to make program output the amount of years and months, how to?

}
}

Greatly appreciated if y'all can help
0
3 years ago
#2
Hint: integer division and remainders.

Do not use an if statement for this as its not scalable. What if someone inputs 123456 as months?

Edit: also you get the error because you areusing == for assignment, use = for assignment and == for checking equality
1
3 years ago
#3
Is there a java equivalent of modulus? If so there's your answer.
1
3 years ago
#4
Have a look into the modulus (remainder) operator: http://www.dreamincode.net/forums/to...dulo-operator/
1
#5
Okay, briefly looked at modulo, I'm a bit confused as to how I could implement that into my code.
0
3 years ago
#6
(Original post by uali511)
Okay, briefly looked at modulo, I'm a bit confused as to how I could implement that into my code.
Think back to doing clock arithmetic in your maths lessons. Modulo arithmetic is the same idea. On a clock face, 11 + 4 = 3 (four hours after 11 o'clock is 3 o'clock). In Java code, if you wrote something like var = (11 + 4) % 12, the result would be 3.

If you modulo by 24 instead of 12, you can work out how many whole days you have, plus the remainder amount in hours:

Code:
```int var, hours, days;
var = 52;
hours = var % 24; // 4
days   = (var - hours) / 24; // (52 - 4)/24 = 2```
For months and years the same idea applies. Use modulus to find the remainder, then subtract from the original value to get a number exactly divisible by 12 (the number of months in a year).
0
3 years ago
#7
(Original post by Dez)
Think back to doing clock arithmetic in your maths lessons. Modulo arithmetic is the same idea. On a clock face, 11 + 4 = 3 (four hours after 11 o'clock is 3 o'clock). In Java code, if you wrote something like var = (11 + 4) % 12, the result would be 3.

If you modulo by 24 instead of 12, you can work out how many whole days you have, plus the remainder amount in hours:

Code:
```int var, hours, days;
var = 52;
hours = var % 24; // 4
days   = (var - hours) / 24; // (52 - 4)/24 = 2```
For months and years the same idea applies. Use modulus to find the remainder, then subtract from the original value to get a number exactly divisible by 12 (the number of months in a year).
Technically you don't need to subtract if you are using integer divison.
0
#8
I'm still confused lmao.
Can someone simply walk me through it?
0
3 years ago
#9
(Original post by uali511)
I'm still confused lmao.
Can someone simply walk me through it?
I'm not sure what else I can say to be honest. Have you tried using the modulus operator? Maybe if you try a few examples you'll be able to get the gist of it.
0
#10
From my code, tell me how to use th operator. I'm pretty new to java so if you actually show me it would help...?
0
3 years ago
#11
0
#12
Do any of you even know how to Java? I could really use some help but none of you are telling me how to use the mudulus operator in relation to my program.
0
3 years ago
#13
because it's better for you to work out yourself than to be spoonfed the answer.
0
3 years ago
#14
(Original post by uali511)
Do any of you even know how to Java? I could really use some help but none of you are telling me how to use the mudulus operator in relation to my program.
Divide by 12 to get number of years, mod to get number of months. It's not hard.
0
#15
Still haven't told me how to actually set it out???
I'm a beginner..
0
3 years ago
#16
(Original post by uali511)
Still haven't told me how to actually set it out???
I'm a beginner..
No one is going to do your work for you - do a bit of research online.
0
#17
>
I have but It's all gobbledegook, i need a walkthrough tbh
0
3 years ago
#18
(Original post by uali511)
>
I have but It's all gobbledegook, i need a walkthrough tbh
Can't you talk to whoever set the assignment...
0
#19
Yes, they've said to me to read through other examples but I still do not understand?
0
3 years ago
#20
Before writing the code get it clear in your head the problem and how you plan to solve it.
Input: Person inputs a certain number of months.
Expected output: Number of years and months.
Step 1) Howd we calculate the number of years ? Divide the number of months by 12.
Step2) How we get the remaining months ? Well we know a yr is 12 months and we calculated the number of years so we could convert the years we calculated to months and subtract that from the number of months they entered. The result would be the months remaining excluding the years.
.Psuedocode:
Code:
```int userEnteredMonths= readMonthInput();
int years = userEnteredMonths / 12;
int months = userEnteredMonths - ( years *12);```
0
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Are you travelling in the Uni student travel window (3-9 Dec) to go home for Christmas?

Yes (120)
28.1%
No - I have already returned home (57)
13.35%
No - I plan on travelling outside these dates (84)
19.67%
No - I'm staying at my term time address over Christmas (40)
9.37%
No - I live at home during term anyway (126)
29.51%