• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Manual Calendar

 
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can't believe I forgot that. thanks!
now I just need to fix up that second part.
 
Marshal
Posts: 80745
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 80745
    486
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Sheriff
    Posts: 22850
    132
    Eclipse IDE Spring Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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):
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic