java - 方法在不应该被调用时被调用
问题描述
我目前正在为大学课程制作日历。我之前制作了一个当时功能齐全的日历。在这个版本中,我要触摸日历并制作菜单,我做到了。所以基本上我将旧日历代码复制粘贴到我的代码中,因为这是他想要的相同最终结果。
这就是问题所在:当它应该等待用户输入时,一个名为 monthFromDate 的方法将字符串缩减为大小并将其转换为整数,但它却被过早地调用了。我怀疑 dayFromDate 也可能发生同样的情况。
这是完整的代码:
import java.util.*;
class SuperGlobal {
public static Scanner s = new Scanner(System.in);
public static Calendar c = Calendar.getInstance();
public static boolean InProgress = false;
}
public class MBAssignment2 extends SuperGlobal{
public static void main(String args[]) {
boolean Status = true, Error = false;
while (Status == true) {
System.out.println("Please choose a command");
System.out.printf("\t\"e\" to enter a date and display the corresponding calendar.%n");
System.out.printf("\t\"t\" to get todays date and display todays calandar.%n");
System.out.printf("\t\"n\" to display the next month.%n");
System.out.printf("\t\"p\" to display the previous month.%n");
System.out.printf("\t\"q\" to quit program.%n");
var Command = s.next();
do {
if (Command instanceof String) {
Error = false;
if (Command.equals("e")) {
UserDate();
InProgress = true;
}
else if (Command.equals("t")) {
System.out.println("Coming soon...");
}
else if (Command.equals("n")) {
if (InProgress == true) {
System.out.println("Coming soon...");
}
else {
System.out.println("Please have a calendar displayed.");
Error = true;
}
}
else if (Command.equals("p")) {
if (InProgress == true) {
System.out.println("Coming soon...");
}
else {
System.out.println("Please have a calendar displayed.");
Error = true;
}
}
else if (Command.equals("q")) {
System.out.println("Exiting...");
Status = false;
Error = false;
}
else {
System.out.println("Please enter a valid command.");
Error = true;
}
}
if (Error == true) {
System.out.println("Please choose a command");
System.out.printf("\t\"e\" to enter a date and display the corresponding calendar.%n");
System.out.printf("\t\"t\" to get todays date and display todays calandar.%n");
System.out.printf("\t\"n\" to display the next month.%n");
System.out.printf("\t\"p\" to display the previous month.%n");
System.out.printf("\t\"q\" to quit program.%n");
Command = s.next();
}
} while(Error == true);
}
}
public static void UserDate (){
int cm = c.get(Calendar.MONTH); // Get and store current month value
int cd = c.get(Calendar.DATE); // Get and store current day value
System.out.println("Desired date(mm/dd)-->"); // User input (As a String)
String inp = s.nextLine();
if (inp instanceof String) {
int month = monthFromDate(inp); // Get month value via sending user input to seperate method
int day = dayFromDate(inp); // Get day value via sending user input to seperate method
if (month > 12 || day > 31) { // Check for valid user input
System.out.print("|===INVALID DATE===|");
}
else {
drawMonth(month); // Call method to create calendar with user input
displayDate(month, day); // Write users month and date via seperate method under calender
System.out.println("This month: ");
drawMonth(cm); // Draw current months calendar
displayDate(cm, cd); // Write current month and date under second calendar
}
}
}
public static void drawMonth(int month) { // Method to draw calendars
System.out.println("_____/\\/\\/\\/\\^*^*^*^*^*^*``````~~-#-~~``````*^*^*^*^*^*^/\\/\\/\\/\\_____"); // ASCII Art
System.out.print(" ");
System.out.print(month); // Month value
System.out.printf(" %n");
for(int i = 0; i < 70; i++) { // Row barrier
System.out.print("=");
}
System.out.printf("%n"); // Line break
for(int i = 0; i < 5; i++) { // Call row method 5 times (send which row it is every iteration)
drawRow(i);
}
}
public static int monthFromDate(String date) {
int cut = date.indexOf("/"); // Find slash in user input string
String m1 = date.substring(0, cut); // Isolate month portion of user input
int m2 = Integer.parseInt(m1); // Convert string to Integer
return(m2); // Return month value
}
public static int dayFromDate(String date) {
int cut = date.indexOf("/") + 1; // Find slash and offset one spot to the right
String d1 = date.substring(cut); // Isolate day value
int d2 = Integer.parseInt(d1); // Convert string to Integer
return(d2); // Return day value
}
public static void drawRow(int row) {
int rn = row * 7 + 1; // Calculation of which row (i.e. loop number 3 (i = 2) so 2 * 7 = 14 + 1 = 15)
String check = " "; // Buffer String to make the calendar look nice
for(int i = 0; i < 7; i++) { // Loop to draw row numbers
if (rn > 9) { // Check to see if row number is above 9 (double digits)
check = ""; // If so, remove a space to keep total length the same
}
else {
check = " "; // Reset for Buffer
}
System.out.print("| " + rn + " " + check + " "); // Print first row of a singular day
rn++; // Row number + 1
if(rn > 31){ // Check to see if row number went above 31
rn = 1; // If so, reset row number to one
}
}
System.out.println("|"); // End of row vertical line cap
for(int i = 0; i < 4; i++) { // Nested for loops to finish a row
for(int ii = 0; ii < 7; ii++) {
System.out.print("| ");
}
System.out.println("|");
}
for(int i = 0; i < 70; i++) { // Another for loop for horizontal row barrier
System.out.print("=");
}
System.out.printf("%n"); // Break
}
public static void displayDate(int month, int day) {
System.out.println("Month: " + month); // Print month value
System.out.println("Day: " + day); // Print day value
}
}
这是 jgrasp 运行 i/o:
----jGRASP exec: java MBAssignment2
Please choose a command
"e" to enter a date and display the corresponding calendar.
"t" to get todays date and display todays calandar.
"n" to display the next month.
"p" to display the previous month.
"q" to quit program.
e
Desired date(mm/dd)-->
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 0
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3720)
at java.base/java.lang.String.substring(String.java:1909)
at MBAssignment2.monthFromDate(MBAssignment2.java:103)
at MBAssignment2.UserDate(MBAssignment2.java:73)
at MBAssignment2.main(MBAssignment2.java:22)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
解决方案
推荐阅读
- javascript - 检查弹出窗口是否打开以提供焦点,如果不打开它
- r - 计算R中租金价格的百分比增长?
- android - 将 Java 库与 Kotlin Multiplatform 一起使用
- angular8 - 如何在 Angular 中管理嵌套的 formGroup?
- php - 断开 Laravel 连接
- python - Django - 从 values() 函数返回子级列表?
- vue.js - 使用 vue-cli 和 vue-cli-service 是一种不好的做法吗?
- c++ - 用其他派生类C++重载基类的成员函数
- javascript - 如何在正常函数调用之前显示一些东西而不是之后停止显示?
- python - Tkinter 按钮颜色