PDA

View Full Version : A Small Java program


looking_to_code
09-26-2009, 06:49 PM
I’m working on a Project that needs to verify that someone is 18 or older. If they are 18 or older it needs to display approved and if not display denied. I have written the code to give the age of a person. I have no idea how to make it display if they are 18 or older approve and if below 18 denied.

Here is what i have so far. Any help is most appreciated .

import java.io.*;
class customers age{
Public static void main (String args [])
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;

System.out.println("This is to verify you are 18 or older");

try{
System.out.println("Please Enter in Your First Name: "):
string firstName = bufRead.readLine ();

System.out.println("please Enter the Year You Were Born; ");
string bornYear = bufRead.readLine();

System.out.println('Please Enter the Current Year: ");
String thisYear = bufRead.readLine();

int bYear = Integer.parseInt(bornYear);
int tYear = Integer.parseInt(thisYear);

int age = tYear-bYear;
System.out.println("Hello " + firstName + "You are " + age + "years old");

}
catch (IOException err) {
System.out.println("Error reading line");
}
}
}

ckeyrouz
09-26-2009, 07:22 PM
In fact there are these problems to correct (around 19), but that can be grouped into the following list:


1- Remove the word age from the declaration of the class.
2- public is with small p not a capital P
3- You end your line with semi-column (;) not with a column (:)
4- String is with a capital S not a small s
5- Your class should be public otherwise it will be taken as protected (default for Java).
6- Strings in java are surrounded by double quotes (") not with single quotes(')

ckeyrouz
09-26-2009, 07:23 PM
Here is the corrected class file.
Note that a java class (thus the filename) should be capital (java convention):

import java.io.*;
class Customers
{
public static void main (String args [])
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("This is to verify you are 18 or older");

try
{
System.out.println("Please Enter in Your First Name: ");
String firstName = bufRead.readLine ();

System.out.println("please Enter the Year You Were Born; ");
String bornYear = bufRead.readLine();

System.out.println("Please Enter the Current Year: ");
String thisYear = bufRead.readLine();

int bYear = Integer.parseInt(bornYear);
int tYear = Integer.parseInt(thisYear);

int age = tYear-bYear;
System.out.println("Hello " + firstName + "You are " + age + " years old");
}
catch (IOException err)
{
System.out.println("Error reading line");
}
}
}

looking_to_code
09-26-2009, 10:41 PM
Thanks for the corrections. now i need to know how to make it verify that some one is 18 or older if they are i need it to display approved and if under 18 denied

ckeyrouz
09-26-2009, 10:45 PM
here you are:
import java.io.*;
class Customers
{
public static void main (String args [])
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("This is to verify you are 18 or older");

try
{
System.out.println("Please Enter in Your First Name: ");
String firstName = bufRead.readLine ();

System.out.println("please Enter the Year You Were Born; ");
String bornYear = bufRead.readLine();

System.out.println("Please Enter the Current Year: ");
String thisYear = bufRead.readLine();

int bYear = Integer.parseInt(bornYear);
int tYear = Integer.parseInt(thisYear);

int age = tYear-bYear;
System.out.println("Hello " + firstName + "You are " + age + " years old");
if(age < 18)
{
System.out.println("Your are under 18.\nYou are not allowed to proceed");
System.exit(1);
}
else
{
System.out.println("Your are over 18.\nYou are most welcome to proceed.");
}
}
catch (IOException err)
{
System.out.println("Error reading line");
}
}
}