1. Are you ready for the Galaxy S20? Here is everything we know so far!

age calculater

Discussion in 'Android Development' started by itgirl2012, Apr 23, 2012.

  1. itgirl2012

    itgirl2012 Lurker
    Thread Starter

    hi .
    i make this app to calculate the age ..
    can any one help me to correct errors

    Code (Text):
    1. [SIZE=2][COLOR=#7f0055]
    2. [SIZE=2][COLOR=#7f0055][B]package[/B] com.sa;[COLOR=#222222][FONT=Times New Roman][/FONT][/COLOR]
    3. [B]import[/B] android.app.Activity;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    4. [B]import[/B] android.os.Bundle;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    5. [B]import[/B] android.view.View;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    6. [B]import[/B] android.widget.Button;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    7. [B]import[/B] android.widget.EditText;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    8. [B]import[/B] android.widget.TextView;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    9. [B]import[/B] android.widget.Toast;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    10. [B]import[/B] java.util.*;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    11. [B]public[/B] [B]class[/B] XaozActivity [B]extends[/B] Activity {[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
    12. [COLOR=#3f5fbf][FONT=Verdana]/** Called when the activity is first created. */[/FONT][/COLOR]
    13. [COLOR=#646464][FONT=Verdana]@Override[/FONT][/COLOR]
    14. [B]public[/B] [B]void[/B][COLOR=#222222][FONT=Verdana] onCreate(Bundle savedInstanceState) {[/FONT][/COLOR]
    15. [B]super[/B].onCreate(savedInstanceState);
    16. [COLOR=#222222][FONT=Verdana]setContentView(R.layout.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]main[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    17. [COLOR=#222222][FONT=Verdana]Button n=(Button)findViewById(R.id.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]sum[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    18. [B]final[/B] EditText e=(EditText)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]editText1[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    19. [B]final[/B] EditText e2=(EditText)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]editText2[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    20. [B]final[/B] EditText e3=(EditText)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]editText3[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    21. [B]final[/B] TextView x=(TextView)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]textView1[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    22. [COLOR=#222222][FONT=Verdana]n.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"push"[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    23. n.setOnClickListener([B]new[/B][COLOR=#222222][FONT=Verdana] View.OnClickListener(){[/FONT][/COLOR]
    24. [COLOR=#646464][FONT=Verdana]@Override[/FONT][/COLOR]
    25. [B]public[/B] [B]void[/B][COLOR=#222222][FONT=Verdana] onClick(View v){[/FONT][/COLOR]
    26. [B]int[/B] year=Integer.[I][COLOR=#222222][FONT=Verdana]parseInt[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](e.getText().toString());[/FONT][/COLOR]
    27. [B]int[/B] day=Integer.[I][COLOR=#222222][FONT=Verdana]parseInt[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](e2.getText().toString());[/FONT][/COLOR]
    28. [B]int[/B] month=Integer.[I][COLOR=#222222][FONT=Verdana]parseInt[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](e3.getText().toString());[/FONT][/COLOR]
    29. [COLOR=#222222][FONT=Verdana]Calendar cd = Calendar.[/FONT][/COLOR][I][COLOR=#222222][FONT=Verdana]getInstance[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]();[/FONT][/COLOR]
    30. [B]if[/B](year > cd.get(Calendar.[I][COLOR=#0000c0][FONT=Verdana]YEAR[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana])){[/FONT][/COLOR]
    31. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Invalid "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    32. [COLOR=#222222][FONT=Verdana]System.[/FONT][/COLOR][I][COLOR=#222222][FONT=Verdana]exit[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](0);}[/FONT][/COLOR]
    33. [B]if[/B](month < 1 || month > 12){
    34. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Please enter monthe between 1 to 12 "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);}[/FONT][/COLOR]
    35. [B]else[/B]{
    36. [COLOR=#222222][FONT=Verdana]month--;[/FONT][/COLOR]
    37. [B]if[/B](year == cd.get(Calendar.YEAR)){
    38. [B]if[/B](month > cd.get(Calendar.MONTH)){
    39. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"invalid month "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    40. [COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
    41. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    42. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    43. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    44. [COLOR=#222222][FONT=Verdana]day = Integer.parseInt(in.readLine());[/FONT][/COLOR]
    45. [B]if[/B](month == 0 || month == 2 || month == 4 || month == 6 || month == 7 ||
    46. [COLOR=#222222][FONT=Verdana]month == 9 || month == 11){[/FONT][/COLOR]
    47. [B]if[/B](day > 31 || day < 1){
    48. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Please enter monthe between 1 to 31 "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    49. [COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
    50. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    51. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    52. [B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](month == 3 || month == 5 || month == 8 || month == 10){[/FONT][/COLOR]
    53. [B]if[/B](day > 30 || day < 1){
    54. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Please enter monthe between 1 to 30 "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    55. [COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
    56. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    57. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    58. [B]else[/B]{
    59. [B]if[/B]([B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar().isLeapYear(year)){[/FONT][/COLOR]
    60. [B]if[/B](day < 1 || day > 29){
    61. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Please enter monthe between 1 to 29 "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    62. [COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
    63. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    64. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    65. [B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](day < 1 || day > 28){[/FONT][/COLOR]
    66. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Please enter monthe between 1 to 28 "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    67. [COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
    68. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    69. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    70. [B]if[/B](year == cd.get(Calendar.YEAR)){
    71. [B]if[/B](month == cd.get(Calendar.MONTH)){
    72. [B]if[/B](day > cd.get(Calendar.DAY_OF_MONTH)){
    73. [COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Invalid Date !"[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
    74. [COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
    75. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    76. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    77. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    78. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    79. Calendar bd = [B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar(year, month, day)[U];[/U][/FONT][/COLOR]
    80. [COLOR=#222222][FONT=Verdana]ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);[/FONT][/COLOR]
    81. [B]if[/B](cd.before([B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar(cd.get(Calendar.YEAR), month, day))){[/FONT][/COLOR]
    82. [COLOR=#222222][FONT=Verdana]ageYears--;[/FONT][/COLOR]
    83. [COLOR=#222222][FONT=Verdana]ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));[/FONT][/COLOR]
    84. [B]if[/B](day > cd.get(Calendar.DAY_OF_MONTH)){
    85. [COLOR=#222222][FONT=Verdana]ageDays = day - cd.get(Calendar.DAY_OF_MONTH);[/FONT][/COLOR]
    86. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    87. [B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](day < cd.get(Calendar.DAY_OF_MONTH)){[/FONT][/COLOR]
    88. [COLOR=#222222][FONT=Verdana]ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;[/FONT][/COLOR]
    89. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    90. [B]else[/B]{
    91. [COLOR=#222222][FONT=Verdana]ageDays = 0;[/FONT][/COLOR]
    92. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    93. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    94. [B]else[/B] [B]if[/B](cd.after([B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar(cd.get(Calendar.YEAR), month, day))){[/FONT][/COLOR]
    95. [COLOR=#222222][FONT=Verdana]ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));[/FONT][/COLOR]
    96. [B]if[/B](day > cd.get(Calendar.DAY_OF_MONTH))
    97. [COLOR=#222222][FONT=Verdana]ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;[/FONT][/COLOR]
    98. [B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](day < cd.get(Calendar.DAY_OF_MONTH)){[/FONT][/COLOR]
    99. [COLOR=#222222][FONT=Verdana]ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;[/FONT][/COLOR]
    100. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    101. [B]else[/B]
    102. [COLOR=#222222][FONT=Verdana]ageDays = 0;[/FONT][/COLOR][COLOR=#222222][FONT=Times New Roman][/FONT][/COLOR]
    103. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    104. [B]else[/B]{
    105. [COLOR=#222222][FONT=Verdana]ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);[/FONT][/COLOR]
    106. [COLOR=#222222][FONT=Verdana]ageMonths = 0;[/FONT][/COLOR]
    107. [COLOR=#222222][FONT=Verdana]ageDays = 0;[/FONT][/COLOR]
    108. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    109. [U][COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR][/U]
    110. [COLOR=#222222][FONT=Verdana]}[U]}[/U] );[/FONT][/COLOR]
    111. [COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
    112. [COLOR=#222222][FONT=Verdana]
    [/FONT][/COLOR]

    [/COLOR][/SIZE][/COLOR][/SIZE]

     



    1. Download the Forums for Android™ app!


      Download

       
  2. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    I've move this to the Applications Developers forums so you can get the help you need.:)
     
  3. jonbonazza

    jonbonazza Android Expert

    What errors are you receiving? Have you done any debugging yourself yet?
     
Loading...

Share This Page

Loading...