0

My application:

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

My setter:

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;

It throws an exception when it ready for the user to enter date and time though. Also, what will happen if user enters 4/7/2013 1:30pm as opposed to 4, 7, 2013, 13, 30? Thanks.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
John
  • 3
  • 2

3 Answers3

0

Code should be like this (just quick idea):

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));
1ac0
  • 2,875
  • 3
  • 33
  • 47
  • You need a promotion... How this may help? The question was how to write `myOwnParserForInputFromConsole`. – Aubin Apr 07 '13 at 18:47
  • Thanks! What do I put in the spot for "myOwnParserForInputFromConsole"? – John Apr 07 '13 at 18:57
  • 1
    you need check if input is in expected format - here you need integer on line. so implement to check that input is integer. – 1ac0 Apr 07 '13 at 19:07
0

From the javadoc, nextInt throws "InputMismatchException - if the next token does not match the Integer regular expression, or is out of range". This means that you cannot blindly call nextInt and hope that the input will be an int.

You probably should read the input as a String and perform checks on that input so see whether it is valid.

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
}

Then, when you have all the inputs and they are known to be valid, then call setWhen.

Obviously you can, instead of throwing an exception, try and read the number again.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

Another way by using the standards:

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()));
   }
}

If you want to use a different Locale, it exists a constructor in DateFormat which handle it.

Aubin
  • 14,617
  • 9
  • 61
  • 84