【发布时间】:2020-08-13 03:40:15
【问题描述】:
private int calculateWeekDay() {
if (this.month == 1 || this.month == 2) {
this.month += 12;
this.year -= 1;
}
int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % 7;
String[] weekDay = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
int [] Array = new int[weekDay.length];
for(int i =0 ; i < weekDay.length ; i++){
Array[i] = Integer.parseInt(weekDay[i]);
}
return h;
}
我真的不知道错误在哪里,调试时得到的只是这个错误: 线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“Monday” 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:580) 在 java.lang.Integer.parseInt(Integer.java:615) 在 javaapplication22.JavaApplication22$Zeller.calculateWeekDay(JavaApplication22.java:112) 在 javaapplication22.JavaApplication22$Zeller.toString(JavaApplication22.java:125) 在 javaapplication22.JavaApplication22.main(JavaApplication22.java:17) C:\Users\salee\AppData\Local\NetBeans\Cache\8.2\executor-sn-ps\run.xml:53:Java 返回:1 构建失败(总时间:5 秒)
如果它有助于更多地发现问题,那就是整个代码:
package javaapplication22;
import java.util.Scanner;
public class JavaApplication22 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Zeller [] obj = new Zeller[5];
for (int i = 1 ; i <= obj.length ; i++){
System.out.print("Enter Valid year (>0) - Valid month [1-12] and Valid day [1-31] for Object " + (i) + ":");
int year = scan.nextInt();
int month = scan.nextInt();
int day = scan.nextInt();
obj[i] = new Zeller(year , month , day);
System.out.println(obj[i].toString());
}
}
public static class Zeller {
private int year;
private int month;
private int day;
private String weekDay;
private static int countLeapYears;
public Zeller() {
year = -1;
month = -1;
day = -1;
}
public Zeller(int y, int m, int d) {
this.year = y;
this.month = m;
this.day = d;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String getWeekDay() {
return weekDay;
}
public static int getCountLeapYears() {
return countLeapYears;
}
public void setYear(int y) {
if (y > 0) {
System.out.println(y);
} else if( y <= 0){
System.out.println(this.year);
}
}
public void setMonth(int m) {
if (m >= 1 && m <= 12) {
System.out.println(m);
} else if (!(m >= 1 && m <=12 )){
System.out.println(this.month);
}
}
public void setDay(int m) {
if( m == 1 || m == 3 ||m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
this.day = 31;
System.out.println(this.day );
}else if( m == 4 || m == 6 || m == 9 || m == 11){
this.day = 30;
System.out.println(this.day);
}else if(m == 2){
if(this.checkLeapYear(year) == true){
this.day = 29;
System.out.println(this.day);
}else {
this.day = 28;
System.out.println(this.day);
}
}else{
System.out.println(this.day);
}
}
private int calculateWeekDay() {
if (this.month == 1 || this.month == 2) {
this.month += 12;
this.year -= 1;
}
int h = (((this.day + (13 * this.month + 1) / 5) + this.year + (this.year / 4) - (this.year / 100) + (this.year / 400)) + 5) % 7;
String[] weekDay = {"Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday", " Sunday"};
int [] Array = new int[weekDay.length];
for(int i =0 ; i < weekDay.length ; i++){
Array[i] = Integer.parseInt(weekDay[i]);
}
return h;
}
public boolean checkLeapYear(int year) {
return true == (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
@Override
public String toString() {
if(this.year != -1 || this.month !=0 ||this.day !=0){
return (" In " + this.getYear() + " / " + this.getMonth() + " / " + this.getDay() + " the day was: " + calculateWeekDay());
}
return ("The Date in this Object was input wrong");
}
}
}
}
提前致谢。
【问题讨论】:
-
您正在尝试将工作日的名称解析为整数。 “星期一”不是整数。
-
错误信息是不言自明的 - 您尝试从无法完成的工作日名称中获取整数值。
-
statement
Array[i] = Integer.parseInt(weekDay[i]);抛出异常,因为我们 weekDay 是一个字符串数组,您正试图将其解析为 Integer -
首先感谢您的快速回复,其次我该如何解决。这就是方法的问题:一个 int calculateWeekDay() 方法使用 Christian Zeller 的公式计算并返回 weekDay 作为 0 到 6 之间的整数:o 如果月份为 1,则设置月份 = 13 和年 = 年 - 1 o 如果月份为 2 则将月份 = 14 和年 = 年 - 1 哦 = ((日 + (13 * (月 + 1) / 5) + 年 + (年 / 4) - (年 / 100) + ( year / 400)) + 5) % 7 oh = 0:周一,1:周二,2:周三,3:周四,4:周五,5:周六,6:周日
标签: java arrays string integer