Help...Why won't this compile?

Why won't this compile?

Here is my code:

import java.util.Calendar;

import javax.swing.JOptionPane;

publicclass GetAge{

publicstaticvoid main(String[] args){

int month=Integer.parseInt(JOptionPane.showInputDialog

(null,"Enter what month(MM) you were born:"));

int day=Integer.parseInt(JOptionPane.showInputDialog

(null,"Enter what day(dd) you were born:"));

int year=Integer.parseInt(JOptionPane.showInputDialog

(null,"Enter what year(yyyy) you were born:"));

Calendar rightNow = Calendar.getInstance();

int year2 = rightNow.get(Calendar.YEAR);

int day2 = rightNow.get(Calendar.DATE);

int month2 = rightNow.get(Calendar.MONTH)+1;

int years = year2-year-1;

int months = month2-month+12;

if(day2 >= day)

int days = day2-day;

else

days = day-day2+8;

JOptionPane.showMessageDialog(null,"You are: " + years

+" years " + months +" months and " + days +" days old.");

}

}

[1914 byte] By [dukefan444a] at [2007-11-27 3:47:15]
# 1
nevermind i got it to work thanks all, but can I get someone to run it a few times and verify this thing spits out the correct answers for your birthday and your relative's birthdays PLEASE?
dukefan444a at 2007-7-12 8:51:01 > top of Java-index,Java Essentials,New To Java...
# 2
to fix the code so it will compile --declare int days;at the top of the code and take out "int" under the if statement.
dukefan444a at 2007-7-12 8:51:01 > top of Java-index,Java Essentials,New To Java...
# 3

What you're doing is using days after the if statement; without curly braces only a valid statement is allowed.

But if you add curly braces, days will be available only in the if block.

So what you should do is:

import java.util.Calendar;

import javax.swing.JOptionPane;

public class GetAge{

public static void main(String[] args){

int month=Integer.parseInt(JOptionPane.showInputDialog

(null,"Enter what month(MM) you were born:"));

int day=Integer.parseInt(JOptionPane.showInputDialog

(null,"Enter what day(dd) you were born:"));

int year=Integer.parseInt(JOptionPane.showInputDialog

(null,"Enter what year(yyyy) you were born:"));

Calendar rightNow = Calendar.getInstance();

int year2 = rightNow.get(Calendar.YEAR);

int day2 = rightNow.get(Calendar.DATE);

int month2 = rightNow.get(Calendar.MONTH)+1;

int years = year2-year-1;

int months = month2-month+12;

int days = 0;//declare here

if(day2 >= day)

days = day2-day; //...and use here

else

days = day-day2+8;//..and here

JOptionPane.showMessageDialog(null,"You are: " + years

+ " years " + months + " months and " + days + " days old.");

}

}

nogoodatcodinga at 2007-7-12 8:51:01 > top of Java-index,Java Essentials,New To Java...
# 4
Whoops. Sorry, by the time I'd posted the reply, you'd already added your stuff :)
nogoodatcodinga at 2007-7-12 8:51:01 > top of Java-index,Java Essentials,New To Java...
# 5
I think the answers are ok, but there are no validations on the input.You could put in any number for the month, day and year and it works, but gives the wrong answer, obviously.And there is no check for someone entering a non-numeric data.
nogoodatcodinga at 2007-7-12 8:51:01 > top of Java-index,Java Essentials,New To Java...