Reputation: 1889
I am trying to retrieve the date format depending on the month, day and year the user enters. It should be called from one function and the logic in another. When I run the code, the questions run but the date is not outputted even though it is being returned to the getArrivalDate method. Any ideas?
CODE:
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.Month;
import java.text.NumberFormat;
import java.time.LocalDate;
public class Reserve {
public static void main(String[] args) {
System.out.println("Enter the requested input");
System.out.println();
//Call Arrival Date Method
getArrivalDate();
}
public static void getArrivalDate() {
// create a Scanner object
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
System.out.print("month (1-12): ");
int month = sc.nextInt();
System.out.print("day (1-31): ");
int day = sc.nextInt();
System.out.print("year: ");
int year = sc.nextInt();
setArrivalDate(choice);
//Users choice
choice = sc.nextLine();
}
} //End getArrivalDate
public static String setArrivalDate(String arrivalDate1) {
String month = "no month";
int day = 0;
int year = 0;
switch(month) {
case "1":
month = "January";
break;
case "2":
month = "February";
break;
case "3":
month = "March";
break;
case "4":
month = "April";
break;
case "5":
month = "May";
break;
case "6":
month = "June";
break;
case "7":
month = "July";
break;
case "8":
month = "August";
break;
case "9":
month = "September";
break;
case "10":
month = "October";
break;
case "11":
month = "November";
break;
case "12":
month = "December";
break;
}
return arrivalDate1 = "Arrival Date: " + month + day + year;
return arrivalDate1;
}
}
Upvotes: 0
Views: 278
Reputation: 127
Try this:
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
System.out.println("Enter the requested input");
//Call Arrival Date Method
getArrivalDate();
}
private static void getArrivalDate() {
// create a Scanner object
Scanner sc = new Scanner(System.in);
String again;
do {
System.out.print("month (1-12): ");
int month = sc.nextInt();
while(month>12){
System.out.print("Month must be between 1-12.\nTry again:");
month=sc.nextInt();
}
System.out.print("day (1-31): ");
int day = sc.nextInt();
System.out.print("year: ");
int year = sc.nextInt();
setArrivalDate(month, day, year);
System.out.println("\nDo you want to continue? (Y/N): ");
again = sc.next();
} while (again.equalsIgnoreCase("Y"));
} //End getArrivalDate
private static void setArrivalDate(int month, int day, int year) {
System.out.print("\nArrival Date: ");
switch (month) {
case 1:
System.out.println("January " + day + " " + year);
break;
case 2:
System.out.println("Feb " + day + " " + year);
break;
case 3:
System.out.println("March " + day + " " + year);
break;
case 4:
System.out.println("April " + day + " " + year);
break;
case 5:
System.out.println("May " + day + " " + year);
break;
case 6:
System.out.println("June " + day + " " + year);
break;
case 7:
System.out.println("July " + day + " " + year);
break;
case 8:
System.out.println("August " + day + " " + year);
break;
case 9:
System.out.println("September " + day + " " + year);
break;
case 10:
System.out.println("October " + day + " " + year);
break;
case 11:
System.out.println("November " + day + " " + year);
break;
case 12:
System.out.println("December " + day + " " + year);
}
}
}
Upvotes: 0
Reputation: 153
You are taking day, month and year as an input but not passing it to setArrivalDate()
method. Even this method is returning some default value you are not printing anything. Here is a workable code:
import java.util.Scanner;
class Reserve {
public static void main(String[] args) {
System.out.println("Enter the requested input");
System.out.println();
// Call Arrival Date Method
getArrivalDate();
}
public static void getArrivalDate() {
// Create a Scanner object
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
System.out.print("month (1-12): ");
int month = sc.nextInt();
System.out.print("day (1-31): ");
int day = sc.nextInt();
System.out.print("year: ");
int year = sc.nextInt();
// To remove a trailing '\n'.
sc.nextLine();
// You have to pass the values you got from user.
System.out.println(setArrivalDate(day, month, year));
// You should again dispaly a message to ask for input again.
System.out.print("Enter 'Y/y' to continue:");
//Users choice
choice = sc.nextLine();
}
} //End getArrivalDate
public static String setArrivalDate(int day, int month, int year) {
String monthStr = "";
switch (month) {
case 1:
monthStr = "January";
break;
case 2:
monthStr = "February";
break;
case 3:
monthStr = "March";
break;
case 4:
monthStr = "April";
break;
case 5:
monthStr = "May";
break;
case 6:
monthStr = "June";
break;
case 7:
monthStr = "July";
break;
case 8:
monthStr = "August";
break;
case 9:
monthStr = "September";
break;
case 10:
monthStr = "October";
break;
case 11:
monthStr = "November";
break;
case 12:
monthStr = "December";
break;
}
return "Arrival Date: " + monthStr + day + year;
}
}
There are lot of things you can handle. First if you setup a proper format you can use SimpleDateFormat
and clean a code little bit. Next thing is that you should be careful when you use sc.nextLine()
after sc.nextInt()
. sc.nextInt()
will take the integer value and leave a newline character behind which is then taken by sc.nextLine()
. Because of this the actual string you wanted to capture is not read.
Upvotes: 0
Reputation: 191854
You need to at least print out the return value
And you're passing in choice
, which is always just "y"... If you want to show the actual date, start by passing in each value
System.out.println(setArrivalDate(year, month, day));
This isn't really correct either return arrivalDate1 = "Arrival Date: "...
. You don't have to return a variable, just return the string like return "Arrival Date:"...
And rather than a switch case, I suggest learning about the DateFormatter
class
Upvotes: 2