我正在努力解决我在将数据存储到变量中所遇到的问题。每当我运行该程序时,它会打印"无效的月份!,0,0和#34;并且它不会存储我在main方法中调用方法调用的任何内容。不确定这里有什么问题。
import java.util.*;
import java.text.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
/******************************************************************
* Back end development for GeoCountDownTimer
*
* @author Jarred
* @version 9/1/16
******************************************************************/
public class GeoCountDownTimer {
/** Variables **/
public int month, day, year;
/** Main method to run program **/
public static void main(String[] args) {
//Make new object of GeoCountDownTimer to test methods in main
GeoCountDownTimer g = new GeoCountDownTimer();
//Creates new object of GeoCountDownTimer
//Provides input "2/10/2096" but doesn't store correctly
GeoCountDownTimer timer1 = new GeoCountDownTimer("2/10/2096");
System.out.println("Date: " + timer1);
}
private GeoCountDownTimer(){
//Instantiate instance variables
month = 0;
day = 0;
year = 0;
}
public GeoCountDownTimer(int month, int day, int year){
//Initializes instance variables with provided values in parameter
this.month = month;
this.day = day;
this.year = year;
}
public void GeoCountDownTimer(GeoCountDownTimer other){
//Initializes variables with other parameter to allow for multiple
//GeoCountDownTimers to be made
this.month = other.month;
this.day = other.day;
this.year = other.year;
}
/******************************************************
* geoDate takes input in format "dd/MM/yyyy" and I am
* attempting to make it function
* @param geoDate
*****************************************************/
public GeoCountDownTimer(String geoDate){
DateFormat parser = new SimpleDateFormat("dd/MM/yyyy");
Date date = parser.parse(geoDate);
int[] numbers = new int[ .length];
for(int i = 0; i < date.length; i++){
numbers[i] = Integer.parseInt(date[i]);
//Potential other option
/**
String[] parts = geoDate.split("/");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
int[] numbers = new int[parts.length];
for(int i = 0; i < parts.length; i++){
numbers[i] = Integer.parseInt(parts[i]);
}
**/
}
public boolean equals(Object other){
//fix
}
}
public int compareTo(GeoCountDownTimer other){
//fix
return day;
}
public void dec (int days){
new GeoCountDownTimer(month, (day - days), year);
}
public void dec(){
new GeoCountDownTimer(month, (day + 1), year);
}
public void inc (int days){
new GeoCountDownTimer(month, (day + days), year);
}
public void inc(){
new GeoCountDownTimer(month, (day + 1), year);
}
public String toString(){
String monthString;
switch(month){
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid Month!";
break;
}
String result = monthString + " " + day + ", " + year;
return result;
}
public String toDateString(){
String result = month + " / " + day + " / " + year;
return result;
}
/******************************************************************
* Getters And Setters
* @return
******************************************************************/
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
答案 0 :(得分:1)
您正在使用该String调用第5个构造函数,但是在此构造函数中,您永远不会设置对象的月,日或年。相反,他们保留了他们初始化的值,对于所有三个值都是0。然后当你去打印对象时,会调用toString方法,但是在switch语句中没有case for month = 0,所以它返回错误。
要在该构造函数中的某处修复此问题,您需要在尝试打印之前为该对象设置this.month,this.day和this.year。