Splitting a string into different array indexes Java?
我正在尝试将一个字符串拆分为不同的数组索引。此字符串来自用户输入(通过
另外,我怎样才能做
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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"); } } |
1 2 3 4 |
然后在计算中使用
更好的是,使用
使用DateTimeFormat在解析日期字符串中显示给一些Java对象,将字符串解析成DATETIME对象,然后访问成员。
1 2 | DateTimeFormatter format = DateTimeFormat.forPattern("MM/dd/yyyy"); DateTime dateTime = format.parseDateTime(DOB); |
这使用Joda时间库。
或者,也可以用类似的方式使用simpledateformat,将其解析为日期对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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. } } |
我注意到您使用键盘输入来识别字符串。如果用户没有输入您期望的内容,它将使您的程序崩溃。(如果你刚开始Java,这很好,你可以再运行它)
你也可以问他们三次,让他们更容易分开,例如:
1 2 3 4 5 6 7 | 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()); |
现在您在一个数组中有三个整数,拆分并准备操作。
如果integer无法将文本输入解析为数字,则会得到一个数字格式异常。