【问题标题】:How to split the string inputted by user如何拆分用户输入的字符串
【发布时间】:2018-01-07 12:54:05
【问题描述】:

这是我们在 Java 编程中的作业。我想知道如何拆分用户输入的字符串。
该程序将要求用户以MM/dd 格式输入他的出生日期。之后,我希望应用程序拆分日期(分隔符是斜线)。添加号码。并根据数字的总和输出生肖。

预期结果:

Please enter your date of birth (MM/dd): 01/06
This is the mmdd value: 106
Result: Capricorn

但是我没有通过以下代码得到这个结果:

public static void main(String[] args) {

    Scanner scan = new Scanner (System.in);
    int mmdd;
    int a;
    int b;

   System.out.print("Please enter your date of birth(MM/dd): ");
   String stringdate = scan.next();

   a = Integer.parseInt(stringdate.split("/") [0]);
   b = Integer.parseInt(stringdate.split("/") [1]);
   mmdd = a + b;

   System.out.println("This is the mmdd value: " + mmdd);

   System.out.print("Result: ");
     if (mmdd >= 321 && mmdd <= 419) {
         System.out.println("ARIES");
     } else if (mmdd >= 420 && mmdd <= 520) {
         System.out.println("TAURUS");
     } else if (mmdd >= 521 && mmdd <= 620) {
         System.out.println("GEMINI");
     } else if (mmdd >= 621 && mmdd <= 722) {
         System.out.println("CANCER");
     } else if (mmdd >= 723 && mmdd <= 822) {
         System.out.println("LEO");
     } else if (mmdd >= 823 && mmdd <= 922) {
         System.out.println("VIRGO");
     } else if (mmdd >= 923 && mmdd <= 1022) {
         System.out.println("LIBRA");
     } else if (mmdd >= 1023 && mmdd <= 1121) {
         System.out.println("SCORPIO");
     } else if (mmdd >= 1122 && mmdd <= 1221) {
         System.out.println("SAGITTARIUS");
     } else if ((mmdd >= 1222 && mmdd <= 1231) || (mmdd >= 11 && mmdd <= 119)) {
         System.out.println("CAPRICORN");
     } else if (mmdd >= 120 && mmdd <= 218) {
         System.out.println("AQUARIUS");
     } else if (mmdd >= 219 && mmdd <= 320) {
         System.out.println("PISCES");
     }
}

【问题讨论】:

  • 你遇到了什么错误?

标签: java string split parseint


【解决方案1】:

a = Integer.parseInt(stringdate.split("/") [0]); 给你1
b = Integer.parseInt(stringdate.split("/") [1]); 给你6

所以实际上你是在求和1 + 6,因为你得到的是7,而不是106

相反,您可以使用:

String stringdate = scan.next();
stringdate = stringdate.replaceAll("[^\\d]+", "");//remove all non digits

mmdd = Integer.parseInt(stringdate);//then parse the result

如果您输入01/06,则mmdd 返回106 而不是7

【讨论】:

    【解决方案2】:

    您将整数的sum 运算符与字符串的concat 运算符混淆了。 它们可能看起来相同 (+),但它们的行为却大不相同。

    在您的特定情况下:如果您希望您的代码按原样工作,您应该将 ab 保留为字符串(而不是将它们解析为整数),然后解析mmdd = a + b 字符串转换成整数。

    【讨论】:

      【解决方案3】:

      您的代码可以重新格式化为

      public class Main{
      
          public static void main( String[] args ){
              LocalDate parse = LocalDate.parse( "11/15" , DateTimeFormatter.ofPattern( "MM/dd" ) );
      //        month * 100 to make your function( 01/06 ) = 106
              int sum = parse.getMonthValue() * 100 + parse.getDayOfMonth();
              System.out.println( sum );
              System.out.println( Zodiac.valueFromSum( sum ) );
          }
      
          enum Zodiac{
              ARIES,
              TAURUS,
              GEMINI,
              CANCER,
              LEO,
              VIRGO,
              LIBRA,
              SCORPIO,
              SAGITTARIUS,
              CAPRICORN,
              AQUARIUS,
              PISCES;
      
              static Zodiac valueFromSum( Integer mmdd ){
                  if( mmdd >= 321 && mmdd <= 419 ){
                      return ARIES;
                  }else if( mmdd >= 420 && mmdd <= 520 ){
                      return TAURUS;
                  }else if( mmdd >= 521 && mmdd <= 620 ){
                      return GEMINI;
                  }else if( mmdd >= 621 && mmdd <= 722 ){
                      return CANCER;
                  }else if( mmdd >= 723 && mmdd <= 822 ){
                      return LEO;
                  }else if( mmdd >= 823 && mmdd <= 922 ){
                      return VIRGO;
                  }else if( mmdd >= 923 && mmdd <= 1022 ){
                      return LIBRA;
                  }else if( mmdd >= 1023 && mmdd <= 1121 ){
                      return SCORPIO;
                  }else if( mmdd >= 1122 && mmdd <= 1221 ){
                      return SAGITTARIUS;
                  }else if( ( mmdd >= 1222 && mmdd <= 1231 ) || ( mmdd >= 11 && mmdd <= 119 ) ){
                      return CAPRICORN;
                  }else if( mmdd >= 120 && mmdd <= 218 ){
                      return AQUARIUS;
                  }else if( mmdd >= 219 && mmdd <= 320 ){
                      return PISCES;
                  }else{
                      throw new IllegalArgumentException();
                  }
              }
          }
      }
      

      【讨论】:

        最近更新 更多