0

我是从一开始就做错了,还是我只是没有意识到我需要做些什么才能将字符串转换为 int 并找到月份部分?

import javax.swing.JOptionPane;

import java.util.Scanner;

public class GoodDateBook

{
  public static void main(String[]args)

  {
    Scanner kybd = new Scanner(System.in);

String allInput = JOptionPane.showInputDialog("Enter a date in the form mm/dd/yyyy");
String monthDayYear = kybd.nextLine();

int month2 = monthDayYear.indexOf("/");
int month = ?
4

2 回答 2

1

拆分输入:

String monthDayYear = "09/19/2013";
String[] parts = monthDayYear.split("/");
int month = Integer.valueOf(parts[0]);
int day = Integer.valueOf(parts[1]);
int year = Integer.valueOf(parts[2]);
于 2013-09-19T06:01:33.347 回答
0

尝试这个

    int mm = Integer.parseInt("09/19/2013".replaceAll(".+/(.+)/.+", "$1"));
于 2013-09-19T06:14:53.127 回答