Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

java - Is there other way of getting an input like date format?

I was hoping and looking for some other ideas or ways to do this. As you can see, I asked the user for the input which is mmddyyyy (05022001 = May 2, 2001) and used substring in order for me to to take the certain input in the index then parse it. Now, What I want to accomplish is to find another way to do this for example is using the formal date formatter which in my case I didn't use. I think date formatter is alot better than this, but I don't get the idea out of it.

Here's what I've got so far. Thanks in advance.

import java.util.*;

public class ZodiaChineseBirthSign {
    public static void main(String [] args) {
       Scanner reader = new Scanner(System.in);
       String mm,dd,yyyy;
       int mm1,dd1,yyyy1;
       
       /**
        * To keep asking for the input from the user I have to use looping
        * which is the while loop.
        * 
        */
       while(true) {
       System.out.print("Enter your birthdate: ");
       String birth = reader.next();
       
       //Substring is to get the specific input of the user.
       mm = birth.substring(0, 2);
       dd = birth.substring(2, 4);
       yyyy = birth.substring(4);
        
       //ParseInt which is used to convert String into Integer in order for me to be able to determine the Animal Sign and Zodiac.
       mm1 = Integer.parseInt(mm);
       dd1 = Integer.parseInt(dd);
       yyyy1 = Integer.parseInt(yyyy);
       
       try {
           if (mm1 < 1 || mm1 > 12 || dd1 < 1 || dd1 > 31) {
               throw new ArrayIndexOutOfBoundsException();
           }
       
       
       switch (mm1) {
            case 1: System.out.println("You were born on January " + dd1 + ", " + yyyy1 ); break;
            case 2: System.out.println("You were born on February " + dd1 + ", " + yyyy1 ); break;
            case 3: System.out.println("You were born on March " + dd1 + ", " + yyyy1 ); break;
            case 4: System.out.println("You were born on April " + dd1 + ", " + yyyy1 ); break;
            case 5: System.out.println("You were born on May " + dd1 + ", " + yyyy1 ); break;
            case 6: System.out.println("You were born on June " + dd1 + ", " + yyyy1 ); break;
            case 7: System.out.println("You were born on July " + dd1 + ", " + yyyy1 ); break;
            case 8: System.out.println("You were born on August " + dd1 + ", " + yyyy1 ); break;
            case 9: System.out.println("You were born on September " + dd1 + ", " + yyyy1 ); break;
            case 10: System.out.println("You were born on October " + dd1 + ", " + yyyy1 ); break;
            case 11: System.out.println("You were born on November " + dd1 + ", " + yyyy1 ); break;
            case 12: System.out.println("You were born on December " + dd1 + ", " + yyyy1 ); break;
       }
       
       switch (yyyy1 % 12) {
            case 0: System.out.println("in the Year of the Monkey"); break;
            case 1: System.out.println("in the Year of the Rooster"); break;
            case 2: System.out.println("in the Year of the Dog"); break;
            case 3: System.out.println("in the Year of the Pig"); break;
            case 4: System.out.println("in the Year of the Rat"); break;
            case 5: System.out.println("in the Year of the Ox"); break;
            case 6: System.out.println("in the Year of the Tiger"); break;
            case 7: System.out.println("in the Year of the Rabbit"); break;
            case 8: System.out.println("in the Year of the Dragon"); break;
            case 9: System.out.println("in the Year of the Snake"); break;
            case 10: System.out.println("in the Year of the Horse"); break;
            case 11: System.out.println("in the Year of the Sheep"); break;
       }
       
       if ((dd1>21 && mm1 == 12) || (dd1<=19 && mm1== 1)){
           System.out.println("under the sign of Capricorn");
       }
       if ((dd1>20 && mm1 ==1) || (dd1<=18 && mm1 == 2)){
           System.out.println("under the sign of Aquarius");
       }
       if ((dd1>19 && mm1 == 2) || (dd1 <=20 && mm1 == 3)){
           System.out.println("under the sign of Pisces");
       }
       if ((dd1>21 && mm1 == 3) || (dd1 <=20 && mm1 == 4)){
           System.out.println("under the sign of Aries");
       }
       if ((dd1>21 && mm1 == 4) || (dd1 <=20 && mm1 == 5)){
           System.out.println("under the sign of Taurus");
       }
       if ((dd1>21 && mm1 == 5) || (dd1<=20 && mm1 == 6)){
           System.out.println("under the sign of Gemini");
       }
       if ((dd1>21 && mm1 == 6) || (dd1<=20 && mm1 == 7)){
           System.out.println("under the sign of Cancer");
       }
       if ((dd1>21 && mm1  == 7) || (dd1<=20 && mm1== 8)){
           System.out.println("under the sign of Leo");
       }
       if ((dd1>21 && mm1 ==8) || (dd1<=22 && mm1 == 9)){
           System.out.println("under the sign of Virgo");
       }
       if ((dd1>23 && mm1 == 9) || (dd1<=20 && mm1 ==10)){
           System.out.println("under the sign of Libra");
       }
       if ((dd1>21 && mm1 == 10) || (dd1<=22 && mm1 == 11)){
           System.out.println("under the sign of Scorpio");
       }
       if ((dd1>23 && mm1 == 11) || (dd1<=20 && mm1 == 12)){
           System.out.println("under the sign of Sagittarius");
       }
       }
       catch (ArrayIndexOutOfBoundsException e) { //Catch-Block which will print(enter correct birthdate) whenever it throws exception error.
           System.out.println("Please enter the correct Birthdate");
       } 
       System.out.println("");
       }
       
       /**
        * Thanks to some of my classmate for giving me some snippets of codes and ideas ^_^
        */
    }  
}
question from:https://stackoverflow.com/questions/65831738/is-there-other-way-of-getting-an-input-like-date-format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should inform the user of what format you expect.

Take the input string and make a parse attempt. Trap for exception in case of faulty input. No need for you to check the ranges of month and day. LocalDate.parse makes those data entry validation checks for you.

DateTimeFormatter f = DateTimeFormatter.of( "MMdduuuu" ) ;
try
{
    LocalDate ld = LocalDate.parse( input ) ;
} 
catch ( DateTimeParseException e ) 
{
    … handle faulty input
}

No need for the switch on month. Use Month#getDisplayName to generate a localized name of the month.

String monthName = ld.getMonth().getLocalizedDisplayName( TextStyle. FULL_STANDALONE , Locale.CANADA_FRENCH ) ;

All this has been covered many many times already on Stack Overflow. Search to learn more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...