将字符串分割成不同的数组索引的Java? [英] Splitting a string into different array indexes Java?

查看:249
本文介绍了将字符串分割成不同的数组索引的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分割字符串成不同的数组索引。这个字符串是从用户输入来(通过 java.util.Scanner中的)和被加载到字符串变量。我怎样才能从字符串输入分割成不同的数组索引?

另外,我怎么能做到这一点是由 DOB 暗示作为数学函数的 INT

下面是我的code:

 进口java.util.Scanner中;公共类主要{
    公共静态无效的主要(字符串ARGS []){
        扫描仪输入=新的扫描仪(System.in);
        的System.out.println(输入出生日期(MM / DD / YYYY):);
        串DOB;
        DOB = input.next();
        INT年龄= 0;
        年龄= 2013 - 出生日期 - 1;
        INT AGE2 = 0;
        AGE2 =年龄+ 1;
        的System.out.println(你是+年龄+或+ AGE2 +岁);
    }
}


解决方案

 的String [] =零件DOB.split(/);
INT个月=的Integer.parseInt(部件[0]);
INT天=的Integer.parseInt(部分[1]);
INT年=的Integer.parseInt(部件[2]);

然后,只需使用而不是 DOB 在你的计算。

更重要的是,使用 新日历() 获得今天的precise日期,和比较针对

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.

这篇关于将字符串分割成不同的数组索引的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆