-2

/* This code reacts negatively when the input is received in the format mm / dd/ yyyy or mm/dd/yyyy. I've asked for help but I think there's more to it than simply a String vs. int syntax dynamic going on. Can you help explain why this code is going awry? /*

import java.util.*;
  import java.lang.*;
  public class kspohn_Numerology{
      private static int month,year,day,sumOfDigits;
      private static boolean isLeapYear = year%4==0||year%400==0|| year==2000;
      private static void DOB(){
      int filler1=0,filler2=0;
      Scanner console = new Scanner(System.in);
      System.out.print("Enter birth date (mm/dd/yyyy):");
      int monthB=console.nextInt();
        char symbol1 =console.next().charAt(0);
        int dayB=console.nextInt();
        char symbol2=console.next().charAt(0);
        int yearB=console.nextInt();
    while(year<1980 || year>2280){
    System.out.printf("Bad year: %d\n",year);
    System.out.print("Enter birth date(mm/dd/yyyy):");
    monthB= console.nextInt();
    symbol1 = console.next().charAt(0);
    dayB = console.nextInt();
    symbol2= console.next().charAt(0);
    yearB = console.nextInt();
}
month=monthB;
day=dayB;
year = yearB;
private static void Confirm(){
    if(month>12 || month<1){
        System.out.printf("Bad Month: %d",month);
    }
     if( (month == 2 && day ==29) && !isLeapYear){
    else if(day>31 || day<0){
        System.out.printf("Bad day:%d",day);
    }
    else{
    }

}
 public static void main (String[] args){
   DOB();
    Confirm();
}
}
Atri
  • 5,511
  • 5
  • 30
  • 40
  • 4
    Is all that code really necessary to demonstrate the issue you're seeing? If not, cut out all of the irrelevant parts. Only post what is necessary. – Sotirios Delimanolis Nov 16 '15 at 22:08
  • What do you mean by "reacts negatively"? Could you please show the error or unexpected output along with sample input? – Atri Nov 16 '15 at 22:12
  • when you input in that format this is the result. Enter birth date (mm/dd/yyyy):03/28/1997 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at kspohn_Numerology.DOB(kspohn_Numerology.java:10) at kspohn_Numerology.main(kspohn_Numerology.java:144) kspohn@loki:~$ n inputted in that format, it responds with – Kevin Spohn Nov 16 '15 at 22:32
  • I've seen bad year errors when the year is completely fine. It is completely dependent on year values and input at this point – Kevin Spohn Nov 16 '15 at 22:46
  • Btw your code won't compile. `month=monthB; day=dayB; year = yearB;` These statements need to be in a method. It would be better to indent code properly, to make it more readable. – Atri Nov 17 '15 at 20:01

1 Answers1

0

When your input is mm/dd/yyyy, you cannot do console.nextInt().
This will invoke a java.util.InputMismatchException, because nextInt() is looking for an integer but it sees 01/01/1990 for example and says it is not an integer.

The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace() (http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)

You can set a custom delimiter console.useDelimiter("/") and then call nextInt(). This won't handle the spaces for you and 01 /01/2001 will give you the same exception.

Preferred way of doing this is:
Treat mm/dd/yyyy as one line of input. You can read it and then split on / like this:

String line = console.nextLine();
String arr[] = line.split("/");
for(String s : arr)
    System.out.println(Integer.parseInt(s));

You can strip the spaces before parsing the value to integer, so that whitespaces are handled.

Atri
  • 5,511
  • 5
  • 30
  • 40