Q:

How can I get multiple characters from a string?

I'm trying to get the 5th, 6th and 7th digits from a list of digits.
E.g. I want to get the year out of the variable dateofbirth, and save it as a separate variable called dob, as an int.
Here is what I have:
int dateofbirth = 17031989
String s = Integer.toString(dateofbirth);
int dob = s.charAt(5);

What would I have to put in the parentheses after s.charAt to get a few digits in a row?

A:

No need for the string conversion:
int dateofbirth = 17031989;
System.out.println(dateofbirth%10000); //1989

If you did want to do it as a string, then the substring() method would be your friend. You'd also need to use Integer.parseInt() to convert the string back into an integer. Taking a character value as an integer will give you the ASCII value of that character, not an integer representing that character!

