2

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");
    }
}

4 Answers 4

5
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.

2

Use DateTimeFormat as shown in Parse Date String to Some Java Object to parse your string into a DateTime object, and then access the members.

DateTimeFormatter format = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime dateTime = format.parseDateTime(DOB);

This uses Joda Time library.

Alternatively you can use SimpleDateFormat in a similar manner, to parse it into a Date object.

0

I notice you're using keyboard input to recognize the string. If the user doesn't input what you expect it will crash your program. (If you're just starting Java, this is fine; you can just run it again)

You can make it easier to split by asking them thrice too eg:

int dob[] = new Integer[3]; // integer array made from Integer class-wrapper
System.out.println("Input day");
dob[0] = Integer.parseInt(input.next()); 
System.out.println("Input month");
dob[1] = Integer.parseInt(input.next());
System.out.println("Input year");
dob[2] = Integer.parseInt(input.next());

You now have three integers in an array, split and ready to manipulate.

If Integer can't parse the text input as a number you'll get a NumberFormatException.

0
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class main {
    public static void main(String args[]) {
        // Man you should look onto doing your
        // homework by yourself, ijs.
        // But here it goes, hope i make myself clear.
        Scanner input = new Scanner(System.in);
        System.out.println("Enter date of birth (MM/DD/YYYY):");
        String DOB;
        DOB = input.next();
        //
        int age;
        // You need to know when it is today. Its not 2013 forever.
        java.util.Calendar cal = java.util.Calendar.getInstance();
        // ^ The above gets a new Calendar object containing system time/date;
        int cur_year = cal.get(Calendar.YEAR);
        int cur_month = cal.get(Calendar.MONTH)+1; // 0-indexed field.
        // Cool we need this info. ill skip the day in month stuff,
        // you do that by your own, okay?
        SimpleDateFormat dfmt = new SimpleDateFormat("MM/dd/yyyy");
        int bir_year;
        int bir_month;
        try {
            // If you wanna program, you must know that not all functions
            // will exit as it's intended. Errors happen and YOU should deal with it.
            // not the user, not the environment. YOU.
            Date d = dfmt.parse(DOB); // This throws a parse exception.
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            bir_year = c.get(Calendar.YEAR);
            bir_month = c.get(Calendar.MONTH)+1; // 0-indexed field;
            age = cur_year - bir_year;
            // Well, you cant be a programmer if you dont think on the logics.
            if(cur_month < bir_month ) {
                age -= 1;
                // If the current month is not yet your birth month or above...
                // means your birthday didnt happen yet in this year.
                // so you still have the age of the last year.
            }
            // If code reaches this point, no exceptions were thrown.
            // and so the code below wont execute.
            // And we have the variable age well defined in memory.
        } catch(ParseException e) {
            // But if the date entered by the user is invalid...
            System.out.println("The date you typed is broken bro.");
            System.out.println("Type a date in the correct format MM/DD/YYYY and retry.");
            return; // Got errors? tell the program to quit the function.
        }
        // Well now we can say to the user how old he is.
        // As if he/she didnt know it ^^'
        System.out.println(String.format("You are %d years old", age));

        // **Not tested.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.