Question:

Please check this program in Java(BlueJ);it's not working!?

by  |  earlier

0 LIKES UnLike

It's to take the day on the 1st Jan of the year,then output the day on any other date of the year.The same input is giving me two different output when I try it twice.Please help!(In BlueJ,we dont need the main function,I can create objects using the interface)

import java.io.*;

public class DateDay

{

InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(ir);

int d,m,y,tot=0,cd,a=0,e,b;

String str;

char arr[]=new char[10];

String x;

public void run()throws IOException

{

System.out.println("Enter the day");

d=Integer.parseInt(br.readLine());

System.out.println("Enter the month");

m=Integer.parseInt(br.readLine());

System.out.println("Enter the year");

y=Integer.parseInt(br.readLine());

System.out.println("Enter the day on 1st");

x=br.readLine();

switch(m)

{

case 1:

tot+=d;

break;

case 2:

tot+=(31+d);

break;

case 3:

if(y%4==0)

{tot+=(31+29+d);}

else{

tot+=(31+28+d);

}break;

case 4:

if(y%4==0)

{tot+=(31+29+31+d);

}

else{

tot+=(31+28+31+d);

}

break;

case 5:

if(y%4==0)

{tot+=(31+29+31+30+d);

}

else{

tot+=(31+28+31+30+d);

}

break;

case 6:

if(y%4==0)

{tot+=(31+29+31+30+31+d);

}

else{

tot+=(31+28+31+30+31+d);

}

break;

case 7:

if(y%4==0)

{tot+=(31+29+31+30+31+30+d);

}

else{

tot+=(31+28+31+30+31+30+d);

}

break;

case 8:

if(y%4==0)

{tot+=(31+29+31+30+31+30+31+d);

}

else{

tot+=(31+28+31+30+31+30+31+d);

}

break;

case 9:

if(y%4==0)

{tot+=(31+29+31+30+31+30+31+31+d);

}

else{

tot+=(31+28+31+30+31+30+31+31+d);

}

break;

case 10:

if(y%4==0)

{tot+=(31+29+31+30+31+30+31+31+30+d);

}

else{

tot+=(31+28+31+30+31+30+31+31+30+d);

}

break;

case 11:

if(y%4==0)

{tot+=(31+29+31+30+31+30+31+31+30+31+d);

}

else{

tot+=(31+28+31+30+31+30+31+31+30+31+d);

}

break;

case 12:

if(y%4==0)

{tot+=(31+29+31+30+31+30+31+31+30+31+30+...

}

else{

tot+=(31+28+31+30+31+30+31+31+30+31+30+d...

}

break;

}

cd=tot%7;

if(x=="Sun")

a=1;

else if(x=="Mon")

a=2;

else if(x=="Tue")

a=3;

else if(x=="Wed")

a=4;

else if(x=="Thu")

a=5;

else if(x=="Fri")

a=6;

else if(x=="Sat")

a=7;

b=a+cd;

if(b>7)

e=b-7;

else

e=b;

switch(e)

{

case 1:

System.out.println("Sumday");

break;

case 2:

System.out.println("Monday");

break;

case 3:

System.out.println("Tuesday");

break;

case 4:

System.out.println("Wednesday");

break;

case 5:

System.out.println("Thursday");

 Tags:

   Report

2 ANSWERS


  1. All right first:

    I can guarantee there is a better algorithm out there. Possibly search lewis caroll date algorithm. Anyway, I am not completely sure how it is supposed to work but it could be because you have the same if() statement about twelve times. The if statement in question is:

    if(y%4==0)

    Well if I want a number above zero and i ask what is 4+3

    and put

    if(answer<0){

    answer = 8

    }

    if(answer<0){

    answer = 6

    }

    Then I could get 8 or 6 for the same input. Hope it helps! Good luck on the script. ☺


  2. wow! im not even guna look at this code! what class are you in? It looks horrible-could be much better structure. Think about it, there must be a better algorithm than what youre using ;)

    neways-you shouldnt get two dif answers for same input, unless youre dealing with random numbers and c**p.

    also, year % 4 will not produce accurate results for leap years. you must remember that in year % 100 = true, then theres no leap year unless year % 400 = true also

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.