使用substring()在Java中进行字符串处理

时间:2017-12-17 05:55:35

标签: java string

在这个程序中,我目前要求用户以mm / dd / yyyy格式输入日期,但我希望能够以m / d / yyyy,mm / d / yyyy格式输入格式,或者m / dd / yyyy。我怎么能这样做?

import java.util.Scanner;
public class BirthdayReminder
{
   public static void main(String[] args)
   {
      // declare variables here

       String[] names = new String[10];
       String inputName;
       String birthday;
       int[] month = new int[10];
       int[] day = new int[10];
       int[] year = new int[10];
       int count = 0;
       final int MAX = 10;
       final String QUIT = "ZZZ";
       String inputMonth;
       String inputDay;
       String inputYear;

       Scanner input = new Scanner(System.in);
       System.out.print("Enter the name of a friend >> ");
       inputName = input.nextLine();
       System.out.print("Enter friend's birthday in format mm/dd/yyyy >> ");
       birthday = input.nextLine();

      // allow user to enter up to 10 friends, first their name, then their birthday with slashes
      // between the month, day, and year, or until they enter ZZZ as a name

       while(inputName.compareTo(QUIT) != 0)
       {
           names[count] = inputName;

           inputMonth = birthday.substring(0, 2);
           inputDay = birthday.substring(3, 5);
           inputYear = birthday.substring(6, 10);

           int intMonth = Integer.parseInt(inputMonth);
           int intDay = Integer.parseInt(inputDay);
           int intYear = Integer.parseInt(inputYear);

           month[count] = intMonth;
           day[count] = intDay;
           year[count] = intYear;

           ++count;

           if(count == MAX)
               inputName = QUIT;
           else
           {
               System.out.print("Enter next friend's name or " + QUIT + " to quit >> ");
               inputName = input.nextLine();

               if(inputName.compareTo(QUIT) !=0)
               {
                   System.out.print("Enter friend's birthday in format mm/dd/yyyy >> ");
                   birthday = input.nextLine();
               }
           }
       }

       System.out.println("You have entered " + count + " names.");
       System.out.println("The names you entered are ");
       for(int y = 0; y < count; ++y)
       {
           System.out.print(names[y] + " ");
       }
      // prompt user to enter names of their friends and display the applicable birthday, until the
      // user enters ZZZ as a name

       if(count != 0)
       {
           System.out.println("\nEnter friend's name to see his/her birthday or ZZZ to quit >> ");
           String friendName = input.nextLine();

           for(int i = 0; i < friendName.length() && friendName.compareTo(QUIT) != 0; i++)
           {
               if(friendName.compareTo(names[i]) == 0)
               {
                   System.out.println("The birthday is " + month[i] + "/" + day[i] + "/" + year[i]);
               }

               else
               {
                   System.out.println("This name has not been previously entered.");
               }
           }
       }
   }
}

我一直在努力做这样的事情

int x = 0;
while(x < birthday.length())
{
   if(birthday.charAt(x) == '/')
   {
      inputMonth = birthday.substring(0, x);
      inputDay = birthday.susbtring(x + 1, something);
      inputYear = birthday.substring(x + something, birthday.length());
   }
   ++x;
}

但是我不知道如何让它工作,因为用户条目将包含两个斜杠而不是一个,因此将有两个x。

P.S。我刚刚学习了基本的java编程。

2 个答案:

答案 0 :(得分:2)

我怀疑这是一个学校问题,证明你可以使用子串而不是使用SimpleDateFormat。

最简单的方法是避免循环,只使用String.indexOf(char,firstIndex)两次来获取第一个和第二个斜杠索引......

int slash1 = birthday.indexOf('/', 0);
int slash2 = birthday.indexOf('/', slash1 + 1);

String inputMonth = birthday.substring(0, slash1);
String inputDay = birthday.substring(slash1 + 1, slash2);
String inputYear = birthday.substring(slash2 + 1, birthday.length()); 

答案 1 :(得分:0)

您可以使用以下代码:

String []myFormat = {"m/d/yyyy" , "mm/d/yyyy" , "m/dd/yyyy"};
    birthday = input.nextLine();
    for(int i=0 ; i < myFormat.length ; i++)
    {
        try
        {
            SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat(myFormat[i]);
            Date date =  datetimeFormatter1.parse(birthday);
            System.out.println(date.toString());
        }
        catch (Exception e)
        {

        }
    }