Hashmap, date, exception Android Club 2015
Agenda Hashmap Date Exception handling
HashMap Unordered list Based on key-value
HashMap: example HashMap countryCodes = new HashMap (); countryCodes.put("UZ", "Uzbekistan"); countryCodes.put("JP", "Japan"); countryCodes.put("US", "United States"); System.out.println(countryCodes.get("UZ" ));
HashMap: practice Create HashMap: elements Key: String Value: String Add values: H – Hydron O – Oxygen N – Nitrogen Print Oxygen
HashMap: practice 2 Create Hashmap: httpCodes Key: int Value: String 404 – Not found 403 – Forbidden 400 – Bad request Print “Not found”
HashMap: practice 3 Create HashMap: capitals Key: String Value: String Uzbekistan – Tashkent Japan – Tokyo US – Washington Print Japan’s capital city
Date Date d = new Date(); System.out.println(d);
SimpleDateFormat: date Date d = new Date(); SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdfDate.format(d));
Documentation va/text/SimpleDateFormat.htmlhttp://docs.oracle.com/javase/8/docs/api/ja va/text/SimpleDateFormat.html
SimpleDateFormat: time SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss"); System.out.println("Time:"+sdfTime.forma t(d));
SimpleDateFormat: practice Show: AM or PM
SimpleDateFormat: practice2 Show hour in 0-23 format: PM 07:35:45 -> 19:35:45
SimpleDateFormat: practice3 Create new SimpleDateFormat: sdfYearWeek Week in year: XX Create new SimpleDateFormat: sdfMonthWeek Week in month: XX
SimpleDateFormat: practice4 Show weekday in number: 1 Show weekday in text: Monday
Error Compile time error Run time error
Compile time error String name = "Joe"
Compile time error: practice Create String: without any value Print String
How to handle: compile Fix it!
Run time error String[] names = {"Joe"}; System.out.println(names[1]);
Run time error: practice Create char array: abc Print index 5 value
Try-catch try { String[] names = {"Joe"}; System.out.println(names[1]); } catch (Exception e) { e.printStackTrace(); }
Try-catch block: practice int x = 5; int y = 0; System.out.println(x/y); System.out.println(x+y);
Try-catch: practice2 String[] names = {"Joe"}; System.out.println(names[-1]);
Debugger bug Debug Debugging Debugger
Debugger: example int sum = 0; for (int i = 0; i < 10; i++) { int square = i^2; sum+=square; } System.out.println(sum);
Debugger: practice Create new class Create main method Calculate sum of numbers from 1 to 10 inclusive Check every step using debugger
Questions? Any questions!
Review 8-4 Hashmap 6-4 Date Chapter 7 – Exception handling
Thank you! Thank you for your attention!