I am having difficulty retrieving a value from a string array.
I need to have it output the name of the day of the week.. i.e. Sunday for the number 0. I have the code perfect for outputting the numbers, but I can’t get it to pull from my array of day names.
import java.util.Scanner;
public class FutureDateJava {
String[] dayStrings = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday" };
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter today's day: ");
int today = input.nextInt();
System.out.println("Enter a day in the future ");
int future = input.nextInt();
int futureDay = (today + future) % 7;
System.out.print("Today is " + today +
" and the future day is " + futureDay);
}
}
Actually access the array:
The
%7is to make sure that a day (eg 10) doesn’t cause an out of bounds exception. However, this isn’t exactly optimal, it assumes that Monday is day 0, but the idea is there.