• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps age calculater

itgirl2012

Lurker
Apr 23, 2012
1
0
hi .
i make this app to calculate the age ..
can any one help me to correct errors

Code:
[SIZE=2][COLOR=#7f0055]
[SIZE=2][COLOR=#7f0055][B]package[/B] com.sa;[COLOR=#222222][FONT=Times New Roman][/FONT][/COLOR]
[B]import[/B] android.app.Activity;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] android.os.Bundle;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] android.view.View;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] android.widget.Button;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] android.widget.EditText;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] android.widget.TextView;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] android.widget.Toast;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]import[/B] java.util.*;[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[B]public[/B] [B]class[/B] XaozActivity [B]extends[/B] Activity {[COLOR=#222222][FONT=Verdana][/FONT][/COLOR]
[COLOR=#3f5fbf][FONT=Verdana]/** Called when the activity is first created. */[/FONT][/COLOR]
[COLOR=#646464][FONT=Verdana]@Override[/FONT][/COLOR]
[B]public[/B] [B]void[/B][COLOR=#222222][FONT=Verdana] onCreate(Bundle savedInstanceState) {[/FONT][/COLOR]
[B]super[/B].onCreate(savedInstanceState);
[COLOR=#222222][FONT=Verdana]setContentView(R.layout.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]main[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[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]
[B]final[/B] EditText e=(EditText)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]editText1[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[B]final[/B] EditText e2=(EditText)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]editText2[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[B]final[/B] EditText e3=(EditText)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]editText3[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[B]final[/B] TextView x=(TextView)findViewById(R.id.[I][COLOR=#0000c0][FONT=Verdana]textView1[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]n.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"push"[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
n.setOnClickListener([B]new[/B][COLOR=#222222][FONT=Verdana] View.OnClickListener(){[/FONT][/COLOR]
[COLOR=#646464][FONT=Verdana]@Override[/FONT][/COLOR]
[B]public[/B] [B]void[/B][COLOR=#222222][FONT=Verdana] onClick(View v){[/FONT][/COLOR]
[B]int[/B] year=Integer.[I][COLOR=#222222][FONT=Verdana]parseInt[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](e.getText().toString());[/FONT][/COLOR]
[B]int[/B] day=Integer.[I][COLOR=#222222][FONT=Verdana]parseInt[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](e2.getText().toString());[/FONT][/COLOR]
[B]int[/B] month=Integer.[I][COLOR=#222222][FONT=Verdana]parseInt[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](e3.getText().toString());[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]Calendar cd = Calendar.[/FONT][/COLOR][I][COLOR=#222222][FONT=Verdana]getInstance[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana]();[/FONT][/COLOR]
[B]if[/B](year > cd.get(Calendar.[I][COLOR=#0000c0][FONT=Verdana]YEAR[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana])){[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Invalid "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]System.[/FONT][/COLOR][I][COLOR=#222222][FONT=Verdana]exit[/FONT][/COLOR][/I][COLOR=#222222][FONT=Verdana](0);}[/FONT][/COLOR]
[B]if[/B](month < 1 || month > 12){
[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]
[B]else[/B]{
[COLOR=#222222][FONT=Verdana]month--;[/FONT][/COLOR]
[B]if[/B](year == cd.get(Calendar.YEAR)){
[B]if[/B](month > cd.get(Calendar.MONTH)){
[COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"invalid month "[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]day = Integer.parseInt(in.readLine());[/FONT][/COLOR]
[B]if[/B](month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || 
[COLOR=#222222][FONT=Verdana]month == 9 || month == 11){[/FONT][/COLOR]
[B]if[/B](day > 31 || day < 1){
[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]
[COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](month == 3 || month == 5 || month == 8 || month == 10){[/FONT][/COLOR]
[B]if[/B](day > 30 || day < 1){
[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]
[COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B]{
[B]if[/B]([B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar().isLeapYear(year)){[/FONT][/COLOR]
[B]if[/B](day < 1 || day > 29){
[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]
[COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](day < 1 || day > 28){[/FONT][/COLOR]
[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]
[COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]if[/B](year == cd.get(Calendar.YEAR)){
[B]if[/B](month == cd.get(Calendar.MONTH)){
[B]if[/B](day > cd.get(Calendar.DAY_OF_MONTH)){
[COLOR=#222222][FONT=Verdana]x.setText([/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Invalid Date !"[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]System.exit(0);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
Calendar bd = [B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar(year, month, day)[U];[/U][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);[/FONT][/COLOR]
[B]if[/B](cd.before([B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar(cd.get(Calendar.YEAR), month, day))){[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageYears--;[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));[/FONT][/COLOR]
[B]if[/B](day > cd.get(Calendar.DAY_OF_MONTH)){
[COLOR=#222222][FONT=Verdana]ageDays = day - cd.get(Calendar.DAY_OF_MONTH);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](day < cd.get(Calendar.DAY_OF_MONTH)){[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B]{
[COLOR=#222222][FONT=Verdana]ageDays = 0;[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B] [B]if[/B](cd.after([B]new[/B][COLOR=#222222][FONT=Verdana] GregorianCalendar(cd.get(Calendar.YEAR), month, day))){[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));[/FONT][/COLOR]
[B]if[/B](day > cd.get(Calendar.DAY_OF_MONTH))
[COLOR=#222222][FONT=Verdana]ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;[/FONT][/COLOR]
[B]else[/B] [B]if[/B][COLOR=#222222][FONT=Verdana](day < cd.get(Calendar.DAY_OF_MONTH)){[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B]
[COLOR=#222222][FONT=Verdana]ageDays = 0;[/FONT][/COLOR][COLOR=#222222][FONT=Times New Roman][/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[B]else[/B]{
[COLOR=#222222][FONT=Verdana]ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageMonths = 0;[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]ageDays = 0;[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[U][COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR][/U]
[COLOR=#222222][FONT=Verdana]}[U]}[/U] );[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]}[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]
[/FONT][/COLOR]

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

 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones