将字符串分割成数组java,将字符串分割成不同的数组索引的Java?

I am trying to split a string into different array indexes. This string is coming from user input (through java.util.Scanner) and is being loaded into a String variable. How can I split the input from the string into different array indexes?

Also, how can I do the math functions that are implied by DOBbeing an int?

Here is my code:

import java.util.Scanner;

public class main {

public static void main(String args[]) {

Scanner input = new Scanner(System.in);

System.out.println("Enter date of birth (MM/DD/YYYY):");

String DOB;

DOB = input.next();

int age = 0;

age = 2013 - DOB - 1;

int age2 = 0;

age2 = age + 1;

System.out.println("You are " + age + " or " + age2 + " years old");

}

}

解决方案

String[] parts = DOB.split("/");

int months = Integer.parseInt(parts[0]);

int days = Integer.parseInt(parts[1]);

int years = Integer.parseInt(parts[2]);

Then just use years instead of DOB in your calculations.

Better yet, use new Calendar() to get today's precise date, and compare against that.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值