Reputation: 3137
I'm a complete beginner with Java, and I've been making simple test-programs to review some of the material I read. The following block of code works incorrectly. It's supposed to accept a Year, Month, and Date from the user, and then create a GregorianCalendar object initialized with the year, month and date. However, when I try to return the GregorianCalendar variable's month, it always gives back the month I initialized the month variable with. I'm not sure why.
import java.util.*;
public class Prac {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Year: ");
int YEAR = input.nextInt();
System.out.print("Enter Month: ");
String MONTH_STRING = input.next();
System.out.print("Enter Date: ");
int DATE = input.nextInt();
int MONTH = 10;
String mon = MONTH_STRING.toLowerCase();
if (mon == "january") {
MONTH = 0;
} else if (mon == "february") {
MONTH = 1;
} else if (mon == "march") {
MONTH = 2;
} else if (mon == "april") {
MONTH =3;
} else if (mon == "may"){
MONTH =4;
} else if (mon == "june"){
MONTH =5;
} else if (mon == "july"){
MONTH =6;
} else if (mon == "august"){
MONTH=7;
} else if (mon == "september"){
MONTH=8;
} else if (mon == "october"){
MONTH=9;
} else if (mon == "november"){
MONTH=10;
} else if (mon == "december"){
MONTH =11;
}
GregorianCalendar entDate = new GregorianCalendar(YEAR,MONTH,DATE);
System.out.println(entDate.get(Calendar.MONTH));
}
}
Also, I'm aware I could have used a switch block, but it gave me strange errors somehow.
Upvotes: 0
Views: 359
Reputation: 338604
The Answer by Shaded is correct and should be accepted. You should generally avoid using ==
comparison with objects, and instead call a method such as equals
or isEqual
.
Furthermore, you could call String::equalsIgnoreCase
method rather than changing the string to be lowercase.
boolean sameIgnoringCase = thisString.equalsIgnoreCase( thatString ) ;
The modern approach uses the modern java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
You may set the month by a number, with sane numbering 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Compare months by using the Month
enum rather than mere numbers or text. That class offers a dozen instances, one for each month of the year.
Month month = ld.getMonth() ; // Get `Month` enum object.
You can switch
on an enum.
switch( month ) {
case JANUARY:
…
case FEBRUARY:
…
default:
…
}
Note that a quirk in Java means you cannot prefix the JANUARY
with Month
(that is, Month.JANUARY
) inside a switch. As a habit, I routinely use the enum class name with the object name, except in a switch
where it is forbidden.
Also, you may find the EnumSet
and EnumMap
handy.
Set< Month > winter = EnumSet.of( Month.DECEMBER , Month.JANUARY , Month.FEBRUARY ) ;
boolean isWinter = winter.contains( myLocalDate.getMonth() ) ;
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 17836
String.equals
You're comparing Strings incorrectly.
The ==
operator checks to see if the Objects are the same -- meaning same memory location and everything.
What you want to do is the String::equals
method.
if(mon.equals("january") {
...
} else if(mon.equals("feburary") {
...
} ...
This will check only to see if the value of the string is equal.
Also, unless you're using java 1.7+ you wouldn't be able to use a switch case for Strings. Not really a part of the question, but still good to know.
Upvotes: 2