开发者

Cannot read and validate proper dates from standard input

开发者 https://www.devze.com 2023-04-12 16:09 出处:网络
I am stuck on this code . The problem says Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and message that indicates whether it is

I am stuck on this code . The problem says

Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid.

The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). the day value dd must be from 1 to a value that is appropriate for the given moth. September, April, June, and November each have 30 days. February has 28 days except for the leap years when it has 29. The remaining months all have 31 days each. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.

This the code I wrote so far. The problem is that whatever dates I put in, the output is always the same. Could you help me find the problem?

import java.util.Scanner;

public class Ch3ProblemNine

{

public static void main(String [ ] args)

{
      System.out.println("Enter the date in mm/dd/yyyy format. ");
    String date="";

       Scanner keyboard = new Scanner(System.in);
      
    int mm = 00;

    int dd = 00;

    int yy = 0000;

    date = keyboard.nextLine();

    boolean isLeapYear;
    mm=0;
    dd=0;
    yy=0;
    isLeapYear=false;
    if(yy%4==0 && (!(yy%100==0) || yy%400==0))
    {
    isLeapYear=true;
    }

    if((mm<12) && (mm>1))
      {
       System.out.println("You have entered an invalid month. Please try again.");
      }
    
      if((dd>31 && dd<1))
      {
       System.out.println("You have entered an invalid day. Please try again.");
      }
      
      if((mm==9 && mm==4 && mm==6 && mm==11) && !(dd==31))
      {
       System.out.println("For the given month, you have entered an invalid day.");
      }
      if((mm==2 && !(dd<29)) && isLeapYear==false)
      {
        System.out.println("You have entered an inva开发者_JS百科lid day for the month of February.");
      }
      if((mm==2 && !(dd<30)) && isLeapYear==true)
      {
       System.out.println("You have entered an invalid day for the month of February.");
      }
      else
      {
       System.out.println("You have entered a valid date in the correct format.");
        
           
            }
            
            
           if (isLeapYear){
           if((mm==2 && !(dd==29)) && isLeapYear==true)
            System.out.println(date + " is a valid date.");
           }
           else
               System.out.println( date + "is not valid month must have 29 days or less.");
            if ((mm ==2) && (dd<=28))
                System.out.println( date + "  is a valid date.");

            }
}


I found this within seconds: http://www.javadb.com/check-if-a-string-is-a-valid-date

Your problem is that you're not putting the "date" string into your "mm dd yy" variables. Your variables are never assigned anything.


You have a decent start to your idea - You can break this up into easy steps:

  1. Read input for month/day/year off the input stream (standard input, in this case)
  2. ???
  3. Check the validity of those inputs (leap year, valid day, etc)

The problem obvious lies in step #2. What you want to do is obtain the day, month, and year into those different int fields you have created. Right now, you read the entire input from standard input as a single line, then check day, month, and year values that are always assigned the value zero.

It may help to avoid worrying about the exact format of the input right now, and instead work on just reading 3 int values off of standard input. Scanner has a handy set of methods for checking specific types including hasNextInt() and nextInt(). Here's a sample of it in action:

//Read the (x,y) coordinate pair from stdin
Scanner keyboard = new Scanner(System.in);
int x, y

if (keyboard.hasNextInt()) {
    x = keyboard.nextInt();
}

if (keyboard.hasNextInt()) {
    y = keyboard.nextInt();
}

//Perform some calculations...
...

With that, you could verify that at least your logic is working properly, even if your code for reading the dates in the proper format isn't completely correct. Ensuring your logic and things of that nature is the first big step.

After that step, another helpful hint would be to think about what delimits (or, divides each portion of the input) the date coming in from the keyboard. Notice the format is in mm/dd/yyyy where each individual int value is separated by a / character. I would look around at Scanner and see what ways it can help you read differently formatted inputs.


public class JavaApplication4 { static int mm=00; static int dd=00; static int yyyy=0000; public static void main(String[] args) {

     java.util.Scanner keyboard= new java.util.Scanner(System.in);
     System.out.println("Enter the date in MM/dd/yyyy format. j");

     System.out.println("ادخل قيمة الشهر");
     mm = Integer.parseInt(keyboard.nextLine());
     if(mm>0&&mm<13){
        System.out.println("قيمة الشهر صحيحة");
     }
     else{
        System.out.println("قيمة الشهر غير صحيحة");
        System.out.println("اعد تشغيل البرنامج");
        System.exit(0);
     }
      System.out.println("ادخل قيمةاليوم");
      dd = Integer.parseInt(keyboard.nextLine());
      if(dd>0&&dd<31){
        System.out.println("قيمة اليوم صحيحة");
     }
      else{
        System.out.println("قيمة اليوم غير صحيحة");
        System.out.println("اعد تشغيل البرنامج");
        System.exit(0);
    }

    System.out.println("ادخل قيمة السنة");
    String year =(keyboard.nextLine());
    if(year.length()==4){
    System.out.println("قيمة السنة صحيحة");
    }
    else{
        System.out.println("قيمة السنة غير صحيحة");
        System.out.println("اعد تشغيل البرنامج");
        System.exit(0);
            }
         }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消