繁体   English   中英

Java:将字符串拆分为单独的字符串和整数变量

[英]Java: Split a string into separate string and integer variables

我一直在寻找答案,但我没有找到一个可以帮助我的人(有人可能知道在哪里指点我)。

我想将一个字符串拆分为一个字符串变量和两个整数变量。 我想知道是否有用户使用我发现的字符串拆分方法并通过其他方式将其用于int变量?

码:

import java.util.Scanner;

public class StringInputStream {
   public static void main (String [] args) {
      Scanner inSS = null;
      String userInput = "Jan 12 1992";
      inSS = new Scanner(userInput);

      String userMonth = "";
      int userDate = 0;
      int userYear = 0;

      // Can modify anything below here
      String[] temp = userInput.split(" ", 2); // "1" means stop splitting after one space
      userMonth = temp[0];
      userDate = temp[1];
      userYear = temp[2];
      // Can modify anything above here

      System.out.println("Month: " + userMonth);
      System.out.println("Date: " + userDate);
      System.out.println("Year: " + userYear);

      return;
   }
}

示例输出:

Month: Jan
Date: 12
Year: 1992

根据您的评论和使用Scanner代码。 我想你想要的

userMonth = inSS.next();
userDate = inSS.nextInt();
userYear = inSS.nextInt();

但是如果您的作业不需要Scanner ,那么您当然可以消除它并使用类似的东西

String userInput = "Jan 12 1992";
String[] tokens = userInput.split("\\s+"); // <-- split on one or more spaces
String userMonth = tokens[0];
int userDate = Integer.parseInt(tokens[1]);
int userYear = Integer.parseInt(tokens[2]);

我不想使用大锤来破解坚果:-)但是根据您在应用程序中提供的输入所需的灵活性,您可以使用解析器来解决问题。 如果您使用下面的语法并将其放入ANTLR ,您可以解析输入字符串并从中提取语义元素。 您甚至可以处理不同的输入排列,例如Jan 1999 121999 Mar 12日,等等。

grammar SimpleMixed;
fragment A:('a'|'A');
fragment B:('b'|'B');
fragment C:('c'|'C');
fragment D:('d'|'D');
fragment E:('e'|'E');
fragment F:('f'|'F');
fragment G:('g'|'G');
fragment H:('h'|'H');
fragment I:('i'|'I');
fragment J:('j'|'J');
fragment K:('k'|'K');
fragment L:('l'|'L');
fragment M:('m'|'M');
fragment N:('n'|'N');
fragment O:('o'|'O');
fragment P:('p'|'P');
fragment Q:('q'|'Q');
fragment R:('r'|'R');
fragment S:('s'|'S');
fragment T:('t'|'T');
fragment U:('u'|'U');
fragment V:('v'|'V');
fragment W:('w'|'W');
fragment X:('x'|'X');
fragment Y:('y'|'Y');
fragment Z:('z'|'Z');

s: userinput EOF;
userinput: month date year
    | month year date
    | year month date
    | year date month
    | date year month
    | date month year;

month: Month;
date: Date;
year: Year;
Month: J A N |  F E B | M A R | A P R | M A Y | J U N | J U L | A U G | S E P      |
O C T | N O V | D E C;

Date: [1-9][0-2]?;

Year: [1-9][0-9][0-9][0-9];


WS  :  [ \t\r\n]+ -> skip;

假设用户提供输入jan 1999 12 使用上面的语法从Antlr生成的解析器解析它之后,您将获得下面的解析树,您可以从中轻松提取语义元素,您可以根据该语义元素推断数据类型并创建适当的对象(例如int为年份和日期和月份的字符串)。

在此输入图像描述

好像你正在处理日期字符串,更好地使用特定的库。

Java 8

import java.time.format.DateTimeFormatter;
import java.time.temporal.*;

TemporalAccessor d = DateTimeFormatter.ofPattern("MMM DD yyyy").parse("Jan 12 1992")
d.get(ChronoField.YEAR); // 1992
d.get(ChronoField.MONTH_OF_YEAR); // 1
d.get(ChronoField.DAY_OF_MONTH); // 12

在Java 8之前(java.text.SimpleDateFormat)

Date d = new SimpleDateFormat("MMM DD yyyy").parse("Jan 12 1992");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.get(Calendar.YEAR); // 1992
c.get(Calendar.MONTH); // 0 - yeah, it starts from 0, embarrassingly
c.get(Calendar.DAY_OF_MONTH); // 12

或者使用JodaTime

DateTimeFormat.forPattern("MMM DD yyyy").parseDateTime("Jan 12 1992");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM