aspose file tools*
The moose likes Beginning Java and the fly likes Calendar.getInstance and String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar.getInstance and String" Watch "Calendar.getInstance and String" New topic
Author

Calendar.getInstance and String

Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
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

Joined: Feb 10, 2009
Posts: 710
Calendar.MONTH is zero-based, so January is 0, February is 1, etc.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3274
    
  52

"Code Bone", please check your private messages regarding an important administrative matter.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
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

Joined: Sep 22, 2010
Posts: 18
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

Joined: Feb 10, 2009
Posts: 710
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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....


Steve
Mos Jeff
Greenhorn

Joined: Sep 15, 2010
Posts: 14
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

Joined: May 03, 2008
Posts: 4664
    
    5

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

Joined: Oct 27, 2005
Posts: 19794
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Feb 10, 2009
Posts: 710
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar.getInstance and String