Java Help For n00b Part II

ChrisCramer247

Platinum Member
Dec 6, 2001
2,171
1
0
Hey Everyone, Need some help. I have to edit this code I've written to work with this following instructions:

Instead of automatically calculating when you will reach full retirement age, change the program to allow the user to input the age at which they would like to retire, calculate the year they will reach that age based on their current age, and then display to the monitor when that will be. HINT: Prompt user for year born, prompt user for age they wish to retire, add year born to wish age for retirement. Display result to screen. Make sure you control for any unrealistic conditions (such as if the desired age of retirement is 20 and they are already 21 or older, you should inform them that they have passed their desired retirement year (but do tell them when it was)).

How can I do this? Can I just use the input and use a subratiction of a display println from the 2 variables? I understand the concepts behind it but not quite sure of the coding, any help is VERY appreciated THANK YOU!

-Chris :)


import java.io.*;
public class Retirement {

//Written by Chris C
//Assignment 4
//This is a program that obtains a name and birth year, and estimates year of retirement
//with input from the keyboard
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void main(String arg[])throws IOException {
String name=null;
int birthYear=0;
name=getName();
birthYear=getBirthYear();
display(name+" will be eligible for full retirement in "+getRetirementYear(birthYear));
}//end main

public static String getName()throws IOException{
prompt("Please enter your name: ");
return keyboard.readLine();
}//close getName

public static int getBirthYear(){
int birthYear=0;
prompt("Please enter the full year you were born (e.g., 1986, not 86: )");
try {
String yearString=keyboard.readLine();
if((yearString.length()<4)|(yearString.length()>4)){
display("Year must be four digits! Sorry!");
exitProgram();
}

birthYear=Integer.parseInt(yearString);
if (birthYear<1922){
display("You have already passed your retirement year!");
exitProgram();
}//close if 1922

if(birthYear>2002){
display("You have not been born yet!");
exitProgram();
}
}//close try

catch(Exception n){
display("You need to enter an integer");
getBirthYear();
}//close catch

return birthYear;
}//close getBirthYear

public static void exitProgram(){
display("Exiting the program");
System.exit(0);
}

public static int getRetirementYear(int birthYear){
int fullRetirementAge = 67;
return birthYear+fullRetirementAge;
}//close getRetirementYear

public static void prompt(String n){
System.out.print(n);
}//close prompt

public static void display(String n){
System.out.println(n);
}//close display
}//close Retirement
 

manly

Lifer
Jan 25, 2000
13,203
3,986
136
Part of the problem is that since the source code isn't pretty printed, it's troublesome to look at.

If I'm more bored later, I'll try and return to this thread and help. ;)
 

m0ti

Senior member
Jul 6, 2001
975
0
0
First off, you shouldn't be checking if the birth year is less than 1922 (where did you get this number?). Don't make this check.

public static int getBirthYear(){
int birthYear=0;
prompt("Please enter the full year you were born (e.g., 1986, not 86: )");
try {
String yearString=keyboard.readLine();
if((yearString.length()<4)|(yearString.length()>4)){
display("Year must be four digits! Sorry!");
exitProgram();
}

birthYear=Integer.parseInt(yearString);
if (birthYear<1922){
display("You have already passed your retirement year!");
exitProgram();
}//close if 1922


Second, you should try following directions.

It's all written in black and white in your assignment.

ASK for the user's birth year (you've done that)
ASK the user for his current age
.
.
.

When you have all the info (reread the assignment to see what stuff you should ask), the retirement year is simply birthyear + desired retirement age.