Java字符串到整数转换

可能重复:
如何在Java中将字符串转换为int?

我想弄清楚如何将其中有整数的字符串转换为整型变量。

码:

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 = input.next();
        String month = DOB.substring(0, 1);
        String day = DOB.substring(3, 4);
        String year = DOB.substring(6, 9);
        int month1 = month;
        int age = 0;
        //age = 2013 - DOB - 1;
        int age2 = 0;
        age2  = age + 1;
        System.out.println("You are " + age + " or " + age2 + " years old");
    }
}

我想将String monthString daySting year变成整数。


使用int month = Integer.parseInt(stringMonth);

链接地址: http://www.djcxy.com/p/20909.html

上一篇: Java String to Integer Conversion

下一篇: Cannot convert String to Integer in Java