• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar.getInstance and String

 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JavaRanch/CodeRanch, looking forward to getting to know Java through this community

Here's my code:




Some strings are in Swedish (if you didn't know )

There're no compilation errors, but there is a "logical error" (or whatever you call it when it can compile and run but not do what you want it to).
I want the "månad" (ie. month) in the dialogue window to display the month name and not the month number. As it is right now the month2 string remains empty for some reason.

I also have one question about the beginning of this source file.
This section:

I tried to change it to:

..but for some weird reason I get the " month2 and res might not be initialized" error in Netbeans (6.9.1)
I don't get it, how come I don't have to initialize the string dayOfWeek2 but I have to initialize the two other strings, they're the same datatype, makes no sense..

As you can see I've yet to finish all the if instructions (only done January, February and September, but shouldn't matter at this point).
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar.MONTH is zero-based, so January is 0, February is 1, etc.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Bone wrote:Hi JavaRanch/CodeRanch, looking forward to getting to know Java through this community

Welcome to the forum!
Code Bone wrote:There're no compilation errors, but there is a "logical error" (or whatever you call it when it can compile and run but not do what you want it to).
I want the "månad" (ie. month) in the dialogue window to display the month name and not the month number. As it is right now the month2 string remains empty for some reason.

Well, you're only testing for month == 1, 2 and 9 which corresponds to February, March and October as Calendar.MONTH is zero based.

Code Bone wrote:..but for some weird reason I get the " month2 and res might not be initialized" error in Netbeans (6.9.1)
I don't get it, how come I don't have to initialize the string dayOfWeek2 but I have to initialize the two other strings, they're the same datatype, makes no sense..

Well, you unconditionally assign dayOfWeek2 to all the days of the week, seeing as you have line terminators -- semicolons -- which shouldn't be there, after each if statement.

In the case of month2, it's only initialized if the month is 1, 2 or 9. Not otherwise.

Make those corrections and get back to use with your improved code.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4498
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Code Bone", please check your private messages regarding an important administrative matter.
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks W. Joe and Darryl,
here's the improved code:



After you told me it was zero-based I remember my teacher talking about it during the lecture.. >_< (d'oh)

However, I still don't understand why the string dayOfWeek2 gets to be declared without the "empty string value" (ie. = "") while the strings month2 and res have to be declared and initialized with the "empty string value"?
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:"Code Bone", please check your private messages regarding an important administrative matter.


Alright, done. Didn't know the nick policy was so strict, in a way it's unique and good, no more silly names I guess. Anyway I amended the name
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I see why. For month2, you know that one of the if conditions will always be true but the compiler doesn't. For all it knows none of them will return true, and then you would be trying to use month2 without initializing it.

As for res, you are getting it because you are doing res += .... Basically, that is like saying res = res + ...., and since res hasn't been initialized you will get an error. If you change the statement to res = ...., then it should work.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Bauer wrote:
However, I still don't understand why the string dayOfWeek2 gets to be declared without the "empty string value" (ie. = "") while the strings month2 and res have to be declared and initialized with the "empty string value"?


The dayOfWeek2 variable gets assigned to unconditionally. There is no situation in which the dayOfWeek2 won't get a value*. The compiler can see that.

The month2 value may not be assigned to, if for instance, month == 13. Probably won't happen, but the compiler can't tell that. It just sees that there are routes where month2 may be left unassigned.


*You actually don't want the dayOfWeek2 variable to be left assigned unconditionally. If your if(...) statements were written correctly, you would get the same warning for that variable as you do for the month2 variable. Darryl already told you the fix....
 
Mos Jeff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a formatting note, if you are looking to clean your code, you may want to use an array instead of all of those if statements.

then you could just get the value from the array:

 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mos Jeff wrote:just a formatting note, if you are looking to clean your code, you may want to use an array

Always considering that Ethan may not have studied arrays yet. I would guess that the switch statement would be taught before arrays (I could be wrong) and the psoted code shows that he isn't yet aware of switch.

I you're really looking to clean up code, there's SimpleDateFormat. Not that recommending its use is appropriate on this thread.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SimpleDateFormat is one option. Another option is use DateFormatSymbols. All arrays can be accessed using the values returned by Calendar.get. So to get the French month name the following code can be used:


@Mos Jeff: your code has one problem: while Calendar.JANUARY is 0, Calendar.SUNDAY is 1. You need to modify the return value (subtract Calendar.SUNDAY) before accessing the array. DateFormatSymbols has taken that into account.
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the advice and cleanup tips. I'm taking things one step at a time though for now so I'll put this question in the backburner and maybe get back to it later when I know Java better.. but thanks.
Gonna go with this explaination for now:
W. Joe Smith wrote:As for res, you are getting it because you are doing res += .... Basically, that is like saying res = res + ...., and since res hasn't been initialized you will get an error. If you change the statement to res = ...., then it should work.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to fix your if (dayOfWeek == n) section of code though. As it is now, every day is Saturday.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Don't forget to fix your if (dayOfWeek == n) section of code though. As it is now, every day is Saturday.


I don't know, I would kind of like every day being Saturday!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic