`Im still learning and I sort of feel like Im doing this all wrong, but I could use some help. I have an assignment needs to have the user set a day of the week, then the program will make the user select an option that will either return the day, return the next day, return the previous day, or add certain days to the day they set(ex: If set the day as Monday and add 4 days, then it will return Friday). I really only need help with the adding days part but any advice on how to make the code better is appreciated.
Im wanting to know if I can assign a int value to a string. For example, if String day equals "Sunday", then int a = 1. I want to assign each day of the week to an int value, then add whatever number the user inputs to the int value, then the sum would be the new day.
If theres a better way to do this please let me know, heres my code(sorry if it looks bad).
import java.util.Scanner;
public class Main
{
public static void main(String\[\] args)
{
Day.userInput();
}
}
class Day
{
static int b;
public static void userInput()
{
Scanner scan = new Scanner(System.in);
// set day of week
System.out.println("Please set the day of the week:");
String day = scan.nextLine();
if(day.equals("sunday") || (day.equals("Sunday")) )
{
b = 1;
}
if(day.equals("monday") || (day.equals("Monday")) )
{
b = 2;
}
if(day.equals("tuesday") || (day.equals("Tuesday")) )
{
b = 3;
}
if(day.equals("wednesday") || (day.equals("Wednesday")) )
{
b = 4;
}
if(day.equals("thursday") || (day.equals("Thursday")) )
{
b = 5;
}
if(day.equals("friday") || (day.equals("Friday")) )
{
b = 6;
}
if(day.equals("saturday") || (day.equals("Saturday")) )
{
b = 7;
}
System.out.println("Enter 1 to return the day.\nEnter 2 to return tomorrows day.\nEnter 3 to return yesterdays day.\nEnter 4 to add days to the current day.\n");
int a = scan.nextInt();
// return day
if(a == 1)
{
System.out.println("The day is " + day);
}
//return next day
if(a == 2)
{
if ( b == 1)
{
System.out.println("The next day is Monday.");
}
if (b == 2)
{
System.out.println("The next day is Tuesday.");
}
if (b == 3)
{
System.out.println("The next day is Wednesday.");
}
if (b == 4)
{
System.out.println("The next day is Thursday.");
}
if (b == 5)
{
System.out.println("The next day is Friday.");
}
if (b == 6)
{
System.out.println("The next day is Saturday.");
}
if (b == 7)
{
System.out.println("The next day is Sunday.");
}
}
//return previous day
if(a == 3)
{
if( b == 1)
{
System.out.println("The previous day was Saturday.");
}
if (b == 2)
{
System.out.println("The previous day was Sunday.");
}
if (b == 3)
{
System.out.println("The previous day was Monday.");
}
if (b == 4)
{
System.out.println("The previous day was Tuesday.");
}
if (b == 5)
{
System.out.println("The previous day was Wednesday.");
}
if (b == 6)
{
System.out.println("The previous day was Thursday.");
}
if (b == 7)
{
System.out.println("The previous day was Friday.");
}
}
// add days
if(a == 4 )
{
System.out.println("Enter the number of days you want to add");
int c = scan.nextInt();
}
}
}
I know a way I can do this but it'll take a massive amount of lines and if statements. `
You can create a
Listof days in order, then useindexOfafter converting the inputStringto lowercase.