Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manual Calendar

 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't believe I forgot that. thanks!
now I just need to fix up that second part.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 33
    Android Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49733
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't need to use the built-in Date class; I was simply pointing out it uses 0 = January.
     
    Kirstie Fran
    Ranch Hand
    Posts: 33
    Android Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20605
    60
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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):
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic