The time intervals "year" and "month", which have been adopted on Terra, are also used on other Federation planets and objects, including the CodeGym spaceship. But new extraterrestrial crew members are often confused by these earthly months. Let's help them: write a program that uses collections to display a number's month given its name.
Month number
- 5
Locked
Comments (13)
- Popular
- New
- Old
You must be signed in to leave a comment
Kent Hervey Software Engineer/Consult at Zeal IT ConsultantsExpert
23 January 2020, 23:07
I used HashMap...it seemed more natural than ArrayList...as in...give a key and get back a value
+5
Devonte A
9 June 2020, 18:09
Yep then you can store the key = Month and Value = month number :)
+1
Vitalina
18 October 2020, 08:26
Yes, I did it with HashMap as well🙂
+1
msantagiulianab
18 September 2019, 20:26
This task only works if I use ArrayList.
It does not work when I try to use Array.
Why?
0
Turner
5 September 2019, 19:35
Requirement #3 requests the use of collections. Which appears to pass verification... er, except if you use the abstract Collection itself. To wit:
Collection months = Arrays.asList(Month.values());
The preceding line of code IS valid (the program runs) yet it fails Codegym verification. No worries though. Please make a note of it if you want. Moving forward.
0
mani
1 July 2019, 10:07
please help me to find errors
package com.codegym.task.task08.task0828;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
Month number
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
ArrayList<String> s = new ArrayList<>();
s.add("january");
s.add("february");
s.add("march");
s.add("april");
s.add("may");
s.add("june");
s.add("July");
s.add("august");
s.add("september");
s.add("october");
s.add("november");
s.add("december");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String m = reader.readLine();
String m1=m.toLowerCase();
//System.out.println(m1);
int i= s.indexOf(m1);
if(i!=-1)
System.out.println(m+" is month "+(i+1));
}
}
+2
Roman
2 July 2019, 06:35
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
0
Adam Odoj
1 December 2021, 12:18
You shouldn´t put months names in lower case. In this task, Code Gym verify not the final frase but months name spelling, so it must to be in correctly and in upper case. My code was giving error until I discovered that the only error I had was "z" instead of "c" in December:-))).
Greetings from Portugal!!!
0
Davor Kandic
13 May 2019, 14:54
Again in the task is given wrong example! Sentence that your program should display is without full-stop(.) !!!
0
Roman
14 May 2019, 06:40
Full-stop refers to the whole sentence. The full-stop in the output example would look like this:
The program reads the name of the month from the keyboard and then displays its number on the screen in a phrase like: "May is month 5.".
0
Davor Kandic
14 May 2019, 09:24
If displayed line should be "May is month 5" then in the conditions should NOT be "May is month 5." , because it is logical that whatever is between quotation marks is part of string.
So, this will generate error when verify:
System.out.println(input + " is month " + num + ".");
And this will pass verification:
System.out.println(input + " is month " + num);
And it is not obvious from conditions text.
0
Roman
15 May 2019, 06:38
The full-stop is situated after the quotation mark, it does not belong to the output!
The program reads the name of the month from the keyboard and then displays its number on the screen in a phrase like: "<output>".
<output> = May is month 5 (w/o full-stop!)
0
Davor Kandic
16 May 2019, 22:10
Indeed it is. Now I see it! But I was sure that I saw inside quotations marks. I am sorry, I was wrong. That's because of a lot staring into the screen, I suppose. Sorry, my mistake.
+1