File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Manual Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Manual Calendar" Watch "Manual Calendar" New topic
Author

Manual Calendar

Kirstie Fran
Ranch Hand

Joined: Feb 16, 2011
Posts: 33

I have to create a calendar for class. It has to manually display the date (day of week, month, date, year) after the user inputs: mm dd yyyy.
I have the input and the verification of the values and all of that good stuff, but i want it to either exit or restart the entire program if the verification fails. this is what i have:

System.out.println("Enter the date in MM DD YYYY format please. The spaces are very important!");
Scanner console= new Scanner(System.in);
int month=console.nextInt();
if (month>= 1 && month<=12) System.out.println("Good Month");
else System.out.println("There are only 12 months!");

the exit, or restart has to go after a message is displayed to the user.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Welcome to the Ranch!

As you probably already know, Java is a statically typed language - all variables have a type, which is fixed at the moment you write your source code. You have declared the variable month as an int so it can contain only integer numbers. An assignment statement like month = "January"; is not going to work, because you cannot assign a string "January" to an int variable. You'll have to create a different variable to hold the name of the month. You could for example call it monthName and make it a String.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kirstie Fran
Ranch Hand

Joined: Feb 16, 2011
Posts: 33

can't believe I forgot that. thanks!
now I just need to fix up that second part.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
You could create an array like thisBeware:
  • 1: The comma after December is permissible but not necessary.
  • 2: January is monthNames[0], not ...[1].
  • 3: This brings you in line with the Date class, where the values for the months start at 0.
  • Kirstie Fran
    Ranch Hand

    Joined: Feb 16, 2011
    Posts: 33

    Campbell Ritchie wrote:You could create an array like thisBeware:
  • 1: The comma after December is permissible but not necessary.
  • 2: January is monthNames[0], not ...[1].
  • 3: This brings you in line with the Date class, where the values for the months start at 0.


  • I have to do everything manually, so the date class doesn't apply, but thanks anyway.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38517
        
      23
    You don't need to use the built-in Date class; I was simply pointing out it uses 0 = January.
    Kirstie Fran
    Ranch Hand

    Joined: Feb 16, 2011
    Posts: 33

    Campbell Ritchie wrote:You don't need to use the built-in Date class; I was simply pointing out it uses 0 = January.


    yeah, i know that's the most straightforward way i could have done it. and i fixed that part of the code already, its the part about exiting/restarting the program that i need to fix.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19684
        
      20

    Campbell Ritchie wrote:You could create an array like thisBeware:
  • 3: This brings you in line with the Date class, where the values for the months start at 0.

  • You can start at 1 by using the same technique DateFormatSymbols uses for weekdays (that strangely enough start at 1):


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
     
    Don't get me started about those stupid light bulbs.
     
    subject: Manual Calendar