0

我的应用程序:

System.out.print("Please enter date (and time): ");
myTask.setWhen(
   input.nextInt(),
   input.nextInt(),
   input.nextInt(),
   input.nextInt(),
   input.nextInt());

我的二传手:

public void setWhen(int year, int month, int date, int hourOfDay, int minute){
    this.year = year;
    this.month = month;
    this.date = date;
    this.hourOfDay = hourOfDay;
    this.minute = minute;

但是,当它准备好让用户输入日期和时间时,它会引发异常。另外,如果用户输入 4/7/2013 1:30pm 而不是 4、7、2013、13、30,会发生什么?谢谢。

4

3 回答 3

0

代码应该是这样的(只是快速的想法):

System.out.print("Please enter date (and time): ");
String inputText;
Scanner scanner = new Scanner(System.in);
inputText = scanner.nextLine();
scanner.close();
//parse input and parsed put as input parameter for your setwhen() method
setWhen(myOwnParserForInputFromConsole(inputText));
于 2013-04-07T18:44:56.223 回答
0

使用标准的另一种方法:

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class Calend {

   public static void main( String[] args ) throws ParseException {
      Calendar cal = Calendar.getInstance();
      DateFormat formatter =
         DateFormat.getDateTimeInstance(
            DateFormat.FULL, DateFormat.FULL );
      DateFormat scanner;
      Date date;

      scanner = DateFormat.getDateTimeInstance(
         DateFormat.SHORT, DateFormat.SHORT );
      date = scanner.parse( "7/4/2013 21:01:05" );
      cal.setTime( date );
      System.out.println( formatter.format( cal.getTime()));

      scanner = DateFormat.getDateTimeInstance(
         DateFormat.MEDIUM, DateFormat.MEDIUM );
      date = scanner.parse( "7 avr. 2013 21:01:05" );
      cal.setTime( date );
      System.out.println( formatter.format( cal.getTime()));

      scanner = DateFormat.getDateTimeInstance(
         DateFormat.FULL, DateFormat.FULL );
      date = scanner.parse( "dimanche 7 avril 2013 21 h 01 CEST" );
      cal.setTime( date );
      System.out.println( scanner.format( cal.getTime()));
   }
}

如果你想使用不同的语言环境,它在 DateFormat 中存在一个构造函数来处理它。

于 2013-04-07T19:06:39.393 回答
0

javadoc中,nextInt抛出“ InputMismatchException- 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围”。这意味着您不能盲目调用nextInt并希望输入是int.

您可能应该将输入读取为 aString并对该输入执行检查,以查看它是否有效。

public static void main(String[] args) throws IOException {
    final Scanner scanner = new Scanner(System.in);
    //read year
    final String yearString = scanner.next();
    final int year;
    try {
        year = Integer.parseInt(yearString);
        //example check, pick appropriate bounds
        if(year < 2000 || year > 3000) throw new NumberFormatException("Year not in valid range");
    } catch (NumberFormatException ex) {
        throw new RuntimeException("Failed to parse year.", ex);
    }
    final String monthString = scanner.next();
    final int month;
    try {
        month = Integer.parseInt(monthString);
        //example check, pick appropriate bounds
        if(month < 1 || month > 12) throw new NumberFormatException("Month not in valid range");
    } catch (NumberFormatException ex) {
        throw new RuntimeException("Failed to parse month.", ex);
    }
    //and the rest of the values
}

然后,当您拥有所有输入并且已知它们有效时,请调用setWhen.

显然,您可以尝试再次读取该数字,而不是抛出异常。

于 2013-04-07T18:56:44.193 回答