Reputation: 83
I have wrote this program and the output isn't working. Could you please help me identify where the error is? I have to use this code not any other from the internet, as we have to construct it from what we understood from the lesson. I am using jgrasp.
----jGRASP exec: javac -g samooras.java
the error I get is
samooras.java:25: error: incompatible types: int cannot be converted to String[]
-2*(year/100))%7+7)%7+1;
^
1 error
----jGRASP wedge2: exit code for process is 1.
Code:
import java.util.Scanner;
public class samooras {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] dayoftheweek = {"Sat", "Sun", "Mon", "Tues", "Wed", "Thur", "Fri"};
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
dayoftheweek = ((day +
(13 * ((month + 9) % 12 + 1) - 1) / 5
+ year % 100
+ year % 100 / 4
+ year / 400
- 2 * (year / 100)) % 7 + 7) % 7 + 1;
System.out.println("the day of the week is: " + dayoftheweek);
}
}
Upvotes: 1
Views: 105
Reputation: 23483
Assuming that you are using that formula to get the day of the week from the array you created, in which you would do:
import java.util.Scanner;
public class samooras {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] dayoftheweekArray = {"Sat", "Sun", "Mon", "Tues", "Wed", "Thur", "Fri"};
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
int dayoftheweekNumber = ((day +
(13 * ((month + 9) % 12 + 1) - 1) / 5
+ year % 100
+ year % 100 / 4
+ year / 400
- 2 * (year / 100)) % 7 + 7) % 7 + 1;
String dayoftheweek = dayoftheweekArray[dayoftheweekNumber];
System.out.println("the day of the week is: " + dayoftheweek);
}
}
Upvotes: 4
Reputation: 527
When you define:
String[] dayoftheweek={"Sat","Sun","Mon","Tues","Wed","Thur","Fri"};
dayoftheweek is an array (that what the [] means). An array contains multiple values of the same type (in this case String). To access an element of the array we use an index(for example dayoftheweek[0] is the String "Sat").
So when you say:
dayoftheweek=((day+ ...
you are computing the index of the day of week in your "dayoftheweek" array.
First of all you can't use the same name so, you should have something like:
int dayIndex = ((day+ ...
Once you have the index, you need to apply it to the array in order to get the actual day of week String:
System.out.println("the day of the week is: " + dayoftheweek[dayIndex]);
Upvotes: 1