13 Replies - 540 Views - Last Post: 12 February 2012 - 05:04 PM Rate Topic: -----

Topic Sponsor:

#1 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Friday13th Question

Posted 07 February 2012 - 07:40 PM

I am trying to find out how to get a user input with a month and year, then find when the next Friday 13th will occur from the date they inputed. I have been told a couple different routes, however being still new to Java, it's a little rough to say the least. Here is what I have so far(it's not much)

import java.util.Calendar;
import java.util.Scanner;

public class name {
public static void main(String[] args){
Scanner input = new Scanner(System.in);

System.out.print("Enter the stuff here");
Int month = input.nextInt();
Int year = input.nextInt();

if (year < year || year > year)
System.out.print("not work")

Calendar calendar = Calendar.getInstance();
   calendar.set(Calendar.YEAR, year);
   calendar.set(Calendar.MONTH, month);
   calendar.set(Calendar.DAY_OF_MONTH, 13);
   calendar.set(Calendar.DAY_OF_WEEK);
			
int Dow = (Calendar.DAY_OF_WEEK);

if (Dow != 6)


That's where I get lost... If anyone can help me with this I would greatly appreciate it! Please bare in mind I am very much a beginner in Java. The main reason I know (very little) about Calendar class is from a little research online trying to help myself with this task I have.

This post has been edited by smohd: 07 February 2012 - 07:51 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: Friday13th Question

#2 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Friday13th Question

Posted 08 February 2012 - 01:48 AM

Since you don't have much code or a clear reason for having any, why don't you describe the approach (or which route) you're pursuing. Better yet, if someone handed you a 2012 calendar and said "Find the next Friday the 13th," what would you do? Write down what you'd do using pencil and paper and then use the steps you take to outline the logic of a program.
Was This Post Helpful? 1
  • +
  • -

#3 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 08 February 2012 - 07:23 AM

Okay, for startes I am trying to learn Java and this is one of the problems put in front of me. The route I am trying to take is take the users year and month input, find whatever day the 13th falls on for that month and verify if it is a Friday or not. If not then go to the next month until the 13th and Friday match. I'll agree that posting the (what is probably incorrect) code in the initial post without giving a clear and outlined approach to what I amtrying to do is a bit WRONG! So that being said, any input on how I could achieve the steps I outlined above? Thanks for the speedy and BLUNT (but needed) response!!
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Friday13th Question

Posted 08 February 2012 - 08:53 AM

You didn't do anything wrong. We just need something to work with.

The approach you outlined is reasonable and fairly easily accomplished using Java's Calendar class. If you're not familiar with the online API resource, now's a good time to become more familiar. Here is the Calendar API. You can find most APIs by Googling and choosing the first result. For example, I Googled "java calendar" and the link I gave you was the first result. You can then look through the class' fields, constructors, and methods for ideas on on how to use that tool to achieve your goals.

For this exercise, you already know about getInstance() and set(). You should also look at add() and get() and how to use them using Calendar's fields. I suggest you experiment with MONTH, DAY_OF_MONTH, DAY_OF_WEEK, and DAY_OF_WEEK_IN_MONTH. You can use those methods to flip the pages of the calendar, add a month, and check whether the 13th of the month is a Friday.

Think also about what input validation you want to do and how to accomplish it. You were trying to do something with the statement,

if (year < year || year > year) {}

but I'm not sure what.
Was This Post Helpful? 1
  • +
  • -

#5 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 08 February 2012 - 09:14 AM

Thanks for the tip. I'll look more into those, and hopefully be able to come up with a working code after work. Since I'm not exactly seasoned with this I'll be using your advice and suggested website as a guide and maybe even find a few examples to help me better understand it. Java has definitely been challenging so far but definately enjoying it!!!
Was This Post Helpful? 0
  • +
  • -

#6 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 11 February 2012 - 10:13 PM

Alright, I've got the code working... atleast partly. What I can't figure out is why this isn't working in "real time" dates. I.e... it doesn't find the 13th Friday of April 2012. Any ideas on where I screwed up??

int dayWeek = 1;
		
	while (dayWeek !=6) {
	
	   Calendar trialCal = Calendar.getInstance();
	
		trialCal.set(Calendar.YEAR, inYear);
		trialCal.set(Calendar.MONTH, inMonth);
		trialCal.get(Calendar.DAY_OF_MONTH, 13);
		trialCal.get(Calendar.DAY_OF_WEEK);
	
		dayWeek = trialCal.get(Calendar.DAY_OF_WEEK);
				
				int dayMonth = trialCal.get(Calendar.DAY_OF_MONTH);
				
				if (dayWeek == 6 && dayMonth == 13) 
					monthS(inMonth, inYear);
				else if (inMonth > 12) {
					inYear++;
					inMonth = 1;
				}
				else
					inMonth++;



I really hope I put the code brackets in the right way this time. Please tell me why this isn't accurate?? Thanks
Was This Post Helpful? 0
  • +
  • -

#7 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 11 February 2012 - 10:38 PM

Oh... and the inMonth and inYear are input variables that I get from the user input
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Friday13th Question

Posted 12 February 2012 - 04:32 AM

When posting snippets, it would be helpful to have some comments or a little explanation about what the snippet does and how it contributes to the program.

The one thing that jumps out at me from what you've posted is that you have a while statement and an enclosed if statement that are contradictory. That if statement will never be true inside that while loop.

This post has been edited by GregBrannon: 12 February 2012 - 04:32 AM

Was This Post Helpful? 2
  • +
  • -

#9 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 12 February 2012 - 05:44 AM

I put my comments in where I left them out the first time. If I remove the "IF Statement", how will I be able to check if the day is the 13 and also a Friday?


[quote name='treo750ii' date='11 February 2012 - 10:13 PM' timestamp='1329023598' post='1549998']
Alright, I've got the code working... atleast partly. What I can't figure out is why this isn't working in "real time" dates. I.e... it doesn't find the 13th Friday of April 2012. Any ideas on where I screwed up??

int dayWeek = 1;
	
        //I put this in to keep adding months to inMonth until the Friday 13 matches..	
	while (dayWeek !=6) {
	
	   Calendar trialCal = Calendar.getInstance();
	
                [b]//Set the calendar to what the user put in for a year and month[/b]
		trialCal.set(Calendar.YEAR, inYear);
		trialCal.set(Calendar.MONTH, inMonth);
		trialCal.get(Calendar.DAY_OF_MONTH, 13);
		trialCal.get(Calendar.DAY_OF_WEEK);
	        
                //assign dayWeek the variable to I can check for 13th' ness
                //and because I am a litte unsure of checking it as the regular 
                //(Calendar.get(Calendar.DAY_OF_MONTH) without converting it
		dayWeek = trialCal.get(Calendar.DAY_OF_WEEK);
				
				int dayMonth = trialCal.get(Calendar.DAY_OF_MONTH);
				
                                 //Check if day week is friday and day of month is 13
				if (dayWeek == 6 && dayMonth == 13) 
					monthS(inMonth, inYear);
                                //If the month gets to 12 adda  year and reset month counter
				else if (inMonth > 12) {
					inYear++;
					inMonth = 1;
				}
				else
					inMonth++;



Was This Post Helpful? 0
  • +
  • -

#10 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 12 February 2012 - 07:06 AM

View PostGregBrannon, on 12 February 2012 - 04:32 AM, said:

When posting snippets, it would be helpful to have some comments or a little explanation about what the snippet does and how it contributes to the program.

The one thing that jumps out at me from what you've posted is that you have a while statement and an enclosed if statement that are contradictory. That if statement will never be true inside that while loop.


THANK YOU VERY MUCH!!! After taking a minute and looking at my code and what you said about the "IF statement", I took it out completely and moved my monthS method to outside the while loop and IT WORKS PERFECTLY!!!
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Friday13th Question

Posted 12 February 2012 - 07:09 AM

Good job! Glad to help, and impressed that you applied the thought necessary to figure out the solution yourself.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6397
  • View blog
  • Posts: 25,956
  • Joined: 06-March 08

Re: Friday13th Question

Posted 12 February 2012 - 04:40 PM

How can you expect a statement like this one
if (year < year || year > year)


to be true in any case ?

is 1951 < 1951 or 1951 > 1951
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: Friday13th Question

Posted 12 February 2012 - 04:43 PM

Yay! pbl's back!
Was This Post Helpful? 1
  • +
  • -

#14 treo750ii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-February 12

Re: Friday13th Question

Posted 12 February 2012 - 05:04 PM

View Postpbl, on 12 February 2012 - 04:40 PM, said:

How can you expect a statement like this one
if (year < year || year > year)


to be true in any case ?

is 1951 < 1951 or 1951 > 1951


YOU ARE ABSOLUTELY RIGHT!!! Thanks ... ill be taking that statement out RIGHT AWAY!! Any post from here on out should be more descriptive and more CORRECT! Thanks for the tip!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1