i'm an IT student. Our Instructor gave us assignment to edit and delete txt file.
how to edit and delete text file in java? can you give me the code ?

Recommended Answers

All 12 Replies

import java.util.*;
import java.io.*;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class tondo {
public static void main(String[] args) throws IOException{

int ans = 1;

 int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 Calendar cd = Calendar.getInstance();

    String fileName = "out.txt";
    PrintWriter outputStream = null;



        do{
try{

        outputStream = new PrintWriter(fileName);
        outputStream = new PrintWriter(new FileOutputStream("outD.txt",true));



        System.out.print(" First Name:");
            Scanner keyboard1 = new Scanner(System.in);
            while (!keyboard1.hasNext("[A-Za-z]+")) {
            System.out.println("Enter Letter only:");
            keyboard1.nextLine();
            }
            String word1 = keyboard1.nextLine();    



System.out.print(" Middle Name:");
    Scanner keyboard2 = new Scanner(System.in);
    while (!keyboard2.hasNext("[A-Za-z]+")) {
    System.out.println("Enter Letter Please:");
    keyboard2.nextLine();
    }
    String word2 = keyboard2.nextLine();
    int i = word2.indexOf(' ');
    int j = word2.lastIndexOf(' ');
    String middle = word2.substring(i+1, i+2);

    System.out.print(" LastName:");
    Scanner keyboard = new Scanner(System.in);
    while (!keyboard.hasNext("[A-Za-z]+")) {
    System.out.println("Enter Letter only");
    keyboard.nextLine();
    }
    String line1 = keyboard.nextLine();





    System.out.print("Gender:");
        Scanner keyboard3 = new Scanner(System.in);
        System.out.println("Male[1], Female[2]");
        while (!keyboard3.hasNext("[1-2]")) {
        System.out.println(" enter number 1 and 2 only.");
        System.out.println("Male[1], Female[2]");
        keyboard3.nextLine();
        }

        int input = keyboard3.nextInt();


        if (input==1){
            outputStream.print("Mr." + "  "+ word1 + "  " + word2 +"  "+ line1 );
            }
        else if(input==2){

            outputStream.print("Mrs/Ms."+ " " +word1 + " " +word2 + "  " +line1);

                    }
        else if (input!=1){
            System.out.println("invalid input");
                    }
        else if(input!=2) {
            System.out.println("invalid input");
                    }


System.out.println(" year of birth : ");
        Scanner info = new Scanner(System.in);
        while (!info.hasNext("[0-M]+")) {
        System.out.println("Enter number Please:");
        info.nextLine();
                        }
        year = info.nextInt();

    if(year > cd.get(Calendar.YEAR)){
        System.out.println("not born yet");
                }

    while(year > cd.get(Calendar.YEAR)){
        year = Integer.parseInt(in.readLine());
      if(year > cd.get(Calendar.YEAR)){
     System.out.println("Invalid date of birth.");

    }
     }

System.out.println(" month of birth  ");
    Scanner info2 = new Scanner(System.in);
    while (!info2.hasNext("[0-M]+")) {
    System.out.println("Please enter a number:");
    info.nextLine();
    }
    month = info2.nextInt();
if(month < 1 || month > 12){

    System.out.println(" enter only  month between 1 to 12.");
while(month < 1 || month > 12){
    month = Integer.parseInt(in.readLine());
if(month < 1 || month > 12){
    System.out.println("only enter month between 1 to 12.");
                                    }
                            }
                        }

else{
    month--;
if(year == cd.get(Calendar.YEAR)){
if(month > cd.get(Calendar.MONTH)){
        System.out.println("Invalid input!");
        System.exit(0);
         }
    }
}

System.out.println(" day of birth  ");
Scanner info3 = new Scanner(System.in);
    while (!info3.hasNext("[0-M]+")) {
        System.out.println("Please enter a number:");
            info3.nextLine();
                    }
    day = info3.nextInt();
        if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
            if(day > 31 || day < 1){
                System.out.println("Please enter day between 1 to 31.");
            while(day > 31 || day < 1){
                day = Integer.parseInt(in.readLine());

                if(day > 31 || day < 1){
                 System.out.println("Please enter day between 1 to 31.");
                        }
                    }
                }
            }


        else if(month == 3 || month == 5 || month == 8 || month == 10){
            if(day > 30 || day < 1){
                System.out.println("Please enter day between 1 to 30.");
            while(day > 30 || day < 1){
                day = Integer.parseInt(in.readLine());
                    if(day > 30 || day < 1){
                     System.out.println("Please enter day between 1 to 30.");

                        }
                    }
                }
            }


        else{
            if(new GregorianCalendar().isLeapYear(year)){
                if(day < 1 || day > 29){
                    System.out.println("Please enter day between 1 to 29.");
            while(day < 1 || day > 29){
                day = Integer.parseInt(in.readLine());
                    if(day < 1 || day > 29){
                     System.out.println("Please enter day between 1 to 29.");
                        }
                    }
                }
            }

            else if(day < 1 || day > 28){
                System.out.println("Please enter day between 1 to 28.");
            while(day < 1 || day > 28){
                day = Integer.parseInt(in.readLine());
                    if(day < 1 || day > 28){
                        System.out.println("Please enter day between 1 to 28.");
                        }
                    }
                }
            }

if(year == cd.get(Calendar.YEAR)){
if(month == cd.get(Calendar.MONTH)){
if(day > cd.get(Calendar.DAY_OF_MONTH)){
System.out.print("Invalid date!");
System.exit(0);
}
}
}
}

catch(FileNotFoundException e){

    System.out.print("Error opening the file" +
    fileName);
    System.exit(0);
                }

catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a legal entry!");
System.out.println("Please enter number.");
System.exit(0);
}

Calendar bd = new GregorianCalendar(year, month, day);

ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageYears--;
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
if(day > cd.get(Calendar.DAY_OF_MONTH)){
ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
}
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else {
ageDays = 0;
}
}
else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
if(day > cd.get(Calendar.DAY_OF_MONTH))
ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else
ageDays = 0;
}
else {
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
ageMonths = 0;
ageDays = 0;
}

    outputStream.println("\t\t\t\t\t" + ageYears + " years");



    outputStream.close();
    System.out.println("Those lines were written to " +fileName);
    ans--;


System.out.println("Try again[1=NO, 2=YES]:");
Scanner scan = new Scanner(System.in);
while (!scan.hasNext("[1-2]")){
    System.out.println("Enter only 1 and 2");
    scan.nextLine();
}

int t = scan.nextInt();

ans = t;

}

while (ans == 2);

}

}

commented: Please format your code properly. -3

import java.util.*;
import java.io.*;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class tondo {
public static void main(String[] args) throws IOException{

int ans = 1;

 int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 Calendar cd = Calendar.getInstance();

    String fileName = "out.txt";
    PrintWriter outputStream = null;



        do{
try{

        outputStream = new PrintWriter(fileName);
        outputStream = new PrintWriter(new FileOutputStream("outD.txt",true));



        System.out.print(" First Name:");
            Scanner keyboard1 = new Scanner(System.in);
            while (!keyboard1.hasNext("[A-Za-z]+")) {
            System.out.println("Enter Letter only:");
            keyboard1.nextLine();
            }
            String word1 = keyboard1.nextLine();    



System.out.print(" Middle Name:");
    Scanner keyboard2 = new Scanner(System.in);
    while (!keyboard2.hasNext("[A-Za-z]+")) {
    System.out.println("Enter Letter Please:");
    keyboard2.nextLine();
    }
    String word2 = keyboard2.nextLine();
    int i = word2.indexOf(' ');
    int j = word2.lastIndexOf(' ');
    String middle = word2.substring(i+1, i+2);

    System.out.print(" LastName:");
    Scanner keyboard = new Scanner(System.in);
    while (!keyboard.hasNext("[A-Za-z]+")) {
    System.out.println("Enter Letter only");
    keyboard.nextLine();
    }
    String line1 = keyboard.nextLine();





    System.out.print("Gender:");
        Scanner keyboard3 = new Scanner(System.in);
        System.out.println("Male[1], Female[2]");
        while (!keyboard3.hasNext("[1-2]")) {
        System.out.println(" enter number 1 and 2 only.");
        System.out.println("Male[1], Female[2]");
        keyboard3.nextLine();
        }

        int input = keyboard3.nextInt();


        if (input==1){
            outputStream.print("Mr." + "  "+ word1 + "  " + word2 +"  "+ line1 );
            }
        else if(input==2){

            outputStream.print("Mrs/Ms."+ " " +word1 + " " +word2 + "  " +line1);

                    }
        else if (input!=1){
            System.out.println("invalid input");
                    }
        else if(input!=2) {
            System.out.println("invalid input");
                    }


System.out.println(" year of birth : ");
        Scanner info = new Scanner(System.in);
        while (!info.hasNext("[0-M]+")) {
        System.out.println("Enter number Please:");
        info.nextLine();
                        }
        year = info.nextInt();

    if(year > cd.get(Calendar.YEAR)){
        System.out.println("not born yet");
                }

    while(year > cd.get(Calendar.YEAR)){
        year = Integer.parseInt(in.readLine());
      if(year > cd.get(Calendar.YEAR)){
     System.out.println("Invalid date of birth.");

    }
     }

System.out.println(" month of birth  ");
    Scanner info2 = new Scanner(System.in);
    while (!info2.hasNext("[0-M]+")) {
    System.out.println("Please enter a number:");
    info.nextLine();
    }
    month = info2.nextInt();
if(month < 1 || month > 12){

    System.out.println(" enter only  month between 1 to 12.");
while(month < 1 || month > 12){
    month = Integer.parseInt(in.readLine());
if(month < 1 || month > 12){
    System.out.println("only enter month between 1 to 12.");
                                    }
                            }
                        }

else{
    month--;
if(year == cd.get(Calendar.YEAR)){
if(month > cd.get(Calendar.MONTH)){
        System.out.println("Invalid input!");
        System.exit(0);
         }
    }
}

System.out.println(" day of birth  ");
Scanner info3 = new Scanner(System.in);
    while (!info3.hasNext("[0-M]+")) {
        System.out.println("Please enter a number:");
            info3.nextLine();
                    }
    day = info3.nextInt();
        if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
            if(day > 31 || day < 1){
                System.out.println("Please enter day between 1 to 31.");
            while(day > 31 || day < 1){
                day = Integer.parseInt(in.readLine());

                if(day > 31 || day < 1){
                 System.out.println("Please enter day between 1 to 31.");
                        }
                    }
                }
            }


        else if(month == 3 || month == 5 || month == 8 || month == 10){
            if(day > 30 || day < 1){
                System.out.println("Please enter day between 1 to 30.");
            while(day > 30 || day < 1){
                day = Integer.parseInt(in.readLine());
                    if(day > 30 || day < 1){
                     System.out.println("Please enter day between 1 to 30.");

                        }
                    }
                }
            }


        else{
            if(new GregorianCalendar().isLeapYear(year)){
                if(day < 1 || day > 29){
                    System.out.println("Please enter day between 1 to 29.");
            while(day < 1 || day > 29){
                day = Integer.parseInt(in.readLine());
                    if(day < 1 || day > 29){
                     System.out.println("Please enter day between 1 to 29.");
                        }
                    }
                }
            }

            else if(day < 1 || day > 28){
                System.out.println("Please enter day between 1 to 28.");
            while(day < 1 || day > 28){
                day = Integer.parseInt(in.readLine());
                    if(day < 1 || day > 28){
                        System.out.println("Please enter day between 1 to 28.");
                        }
                    }
                }
            }

if(year == cd.get(Calendar.YEAR)){
if(month == cd.get(Calendar.MONTH)){
if(day > cd.get(Calendar.DAY_OF_MONTH)){
System.out.print("Invalid date!");
System.exit(0);
}
}
}
}

catch(FileNotFoundException e){

    System.out.print("Error opening the file" +
    fileName);
    System.exit(0);
                }

catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a legal entry!");
System.out.println("Please enter number.");
System.exit(0);
}

Calendar bd = new GregorianCalendar(year, month, day);

ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageYears--;
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
if(day > cd.get(Calendar.DAY_OF_MONTH)){
ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
}
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else {
ageDays = 0;
}
}
else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
if(day > cd.get(Calendar.DAY_OF_MONTH))
ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else
ageDays = 0;
}
else {
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
ageMonths = 0;
ageDays = 0;
}

    outputStream.println("\t\t\t\t\t" + ageYears + " years");



    outputStream.close();
    System.out.println("Those lines were written to " +fileName);
    ans--;


System.out.println("Try again[1=NO, 2=YES]:");
Scanner scan = new Scanner(System.in);
while (!scan.hasNext("[1-2]")){
    System.out.println("Enter only 1 and 2");
    scan.nextLine();
}

int t = scan.nextInt();

ans = t;

}

while (ans == 2);

}

}

import java.util.*;
import java.io.*;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class tondo {
public static void main(String[] args) throws IOException{

int ans = 1;

int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Calendar cd = Calendar.getInstance();

String fileName = "out.txt";
PrintWriter outputStream = null;



    do{

try{

    outputStream = new PrintWriter(fileName);
    outputStream = new PrintWriter(new FileOutputStream("outD.txt",true));



    System.out.print(" First Name:");
        Scanner keyboard1 = new Scanner(System.in);
        while (!keyboard1.hasNext("[A-Za-z]+")) {
        System.out.println("Enter Letter only:");
        keyboard1.nextLine();
        }
        String word1 = keyboard1.nextLine();    

System.out.print(" Middle Name:");
Scanner keyboard2 = new Scanner(System.in);
while (!keyboard2.hasNext("[A-Za-z]+")) {
System.out.println("Enter Letter Please:");
keyboard2.nextLine();
}
String word2 = keyboard2.nextLine();
int i = word2.indexOf(' ');
int j = word2.lastIndexOf(' ');
String middle = word2.substring(i+1, i+2);

System.out.print(" LastName:");
Scanner keyboard = new Scanner(System.in);
while (!keyboard.hasNext("[A-Za-z]+")) {
System.out.println("Enter Letter only");
keyboard.nextLine();
}
String line1 = keyboard.nextLine();





System.out.print("Gender:");
    Scanner keyboard3 = new Scanner(System.in);
    System.out.println("Male[1], Female[2]");
    while (!keyboard3.hasNext("[1-2]")) {
    System.out.println(" enter number 1 and 2 only.");
    System.out.println("Male[1], Female[2]");
    keyboard3.nextLine();
    }

    int input = keyboard3.nextInt();


    if (input==1){
        outputStream.print("Mr." + "  "+ word1 + "  " + word2 +"  "+ line1 );
        }
    else if(input==2){

        outputStream.print("Mrs/Ms."+ " " +word1 + " " +word2 + "  " +line1);

                }
    else if (input!=1){
        System.out.println("invalid input");
                }
    else if(input!=2) {
        System.out.println("invalid input");
                }

System.out.println(" year of birth : ");
Scanner info = new Scanner(System.in);
while (!info.hasNext("[0-M]+")) {
System.out.println("Enter number Please:");
info.nextLine();
}
year = info.nextInt();

if(year > cd.get(Calendar.YEAR)){
    System.out.println("not born yet");
            }

while(year > cd.get(Calendar.YEAR)){
    year = Integer.parseInt(in.readLine());
  if(year > cd.get(Calendar.YEAR)){
 System.out.println("Invalid date of birth.");

}
 }

System.out.println(" month of birth ");
Scanner info2 = new Scanner(System.in);
while (!info2.hasNext("[0-M]+")) {
System.out.println("Please enter a number:");
info.nextLine();
}
month = info2.nextInt();
if(month < 1 || month > 12){

System.out.println(" enter only  month between 1 to 12.");

while(month < 1 || month > 12){
month = Integer.parseInt(in.readLine());
if(month < 1 || month > 12){
System.out.println("only enter month between 1 to 12.");
}
}
}

else{
month--;
if(year == cd.get(Calendar.YEAR)){
if(month > cd.get(Calendar.MONTH)){
System.out.println("Invalid input!");
System.exit(0);
}
}
}

System.out.println(" day of birth ");
Scanner info3 = new Scanner(System.in);
while (!info3.hasNext("[0-M]+")) {
System.out.println("Please enter a number:");
info3.nextLine();
}
day = info3.nextInt();
if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
if(day > 31 || day < 1){
System.out.println("Please enter day between 1 to 31.");
while(day > 31 || day < 1){
day = Integer.parseInt(in.readLine());

            if(day > 31 || day < 1){
             System.out.println("Please enter day between 1 to 31.");
                    }
                }
            }
        }


    else if(month == 3 || month == 5 || month == 8 || month == 10){
        if(day > 30 || day < 1){
            System.out.println("Please enter day between 1 to 30.");
        while(day > 30 || day < 1){
            day = Integer.parseInt(in.readLine());
                if(day > 30 || day < 1){
                 System.out.println("Please enter day between 1 to 30.");

                    }
                }
            }
        }


    else{
        if(new GregorianCalendar().isLeapYear(year)){
            if(day < 1 || day > 29){
                System.out.println("Please enter day between 1 to 29.");
        while(day < 1 || day > 29){
            day = Integer.parseInt(in.readLine());
                if(day < 1 || day > 29){
                 System.out.println("Please enter day between 1 to 29.");
                    }
                }
            }
        }

        else if(day < 1 || day > 28){
            System.out.println("Please enter day between 1 to 28.");
        while(day < 1 || day > 28){
            day = Integer.parseInt(in.readLine());
                if(day < 1 || day > 28){
                    System.out.println("Please enter day between 1 to 28.");
                    }
                }
            }
        }

if(year == cd.get(Calendar.YEAR)){
if(month == cd.get(Calendar.MONTH)){
if(day > cd.get(Calendar.DAY_OF_MONTH)){
System.out.print("Invalid date!");
System.exit(0);
}
}
}
}

catch(FileNotFoundException e){

System.out.print("Error opening the file" +
fileName);
System.exit(0);
            }

catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a legal entry!");
System.out.println("Please enter number.");
System.exit(0);
}

Calendar bd = new GregorianCalendar(year, month, day);

ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageYears--;
ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
if(day > cd.get(Calendar.DAY_OF_MONTH)){
ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
}
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else {
ageDays = 0;
}
}
else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
if(day > cd.get(Calendar.DAY_OF_MONTH))
ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
else if(day < cd.get(Calendar.DAY_OF_MONTH)){
ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
}
else
ageDays = 0;
}
else {
ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
ageMonths = 0;
ageDays = 0;
}

outputStream.println("\t\t\t\t\t" + ageYears + " years");



outputStream.close();
System.out.println("Those lines were written to " +fileName);
ans--;

System.out.println("Try again[1=NO, 2=YES]:");
Scanner scan = new Scanner(System.in);
while (!scan.hasNext("[1-2]")){
System.out.println("Enter only 1 and 2");
scan.nextLine();
}

int t = scan.nextInt();

ans = t;
}

while (ans == 2);
}

}

please use the Code button on your editor to retain the formatting, it's much easier to read that way,
also could you explain what's the problem/errors with the code?

walay lain imba sa kalibutan .. kaiser nato or itatago natin sa pangalan na " RESIAK " . ahahaha
it is useful to me .. thank you RESIAK for sharing this java program .

zeroliken : : can you give me a code that it read,edit,and write in text file ?

RESIAK :: can you explain your code >? .please !!

import java.util.*;
import java.io.*;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class tondo {
  public static void main(String[] args) throws IOException{


int ans = 1;



     int day = 1, month = 0, year = 1, ageYears, ageMonths, ageDays;
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     Calendar cd = Calendar.getInstance();

        String fileName = "out.txt";
        PrintWriter outputStream = null;



            do{
    try{

            outputStream = new PrintWriter(fileName);
            outputStream = new PrintWriter(new FileOutputStream("outD.txt",true));



            System.out.print(" First Name:");
                Scanner keyboard1 = new Scanner(System.in);
                while (!keyboard1.hasNext("[A-Za-z]+")) {
                System.out.println("Enter Letter only:");
                keyboard1.nextLine();
                }
                String word1 = keyboard1.nextLine();    



    System.out.print(" Middle Name:");
        Scanner keyboard2 = new Scanner(System.in);
        while (!keyboard2.hasNext("[A-Za-z]+")) {
        System.out.println("Enter Letter Please:");
        keyboard2.nextLine();
        }
        String word2 = keyboard2.nextLine();
        int i = word2.indexOf(' ');
        int j = word2.lastIndexOf(' ');
        String middle = word2.substring(i+1, i+2);

        System.out.print(" LastName:");
        Scanner keyboard = new Scanner(System.in);
        while (!keyboard.hasNext("[A-Za-z]+")) {
        System.out.println("Enter Letter only");
        keyboard.nextLine();
        }
        String line1 = keyboard.nextLine();





        System.out.print("Gender:");
            Scanner keyboard3 = new Scanner(System.in);
            System.out.println("Male[1], Female[2]");
            while (!keyboard3.hasNext("[1-2]")) {
            System.out.println(" enter number 1 and 2 only.");
            System.out.println("Male[1], Female[2]");
            keyboard3.nextLine();
            }

            int input = keyboard3.nextInt();


            if (input==1){
                outputStream.print("Mr." + "  "+ word1 + "  " + word2 +"  "+ line1 );
                }
            else if(input==2){

                outputStream.print("Mrs/Ms."+ " " +word1 + " " +word2 + "  " +line1);

                        }
            else if (input!=1){
                System.out.println("invalid input");
                        }
            else if(input!=2) {
                System.out.println("invalid input");
                        }


    System.out.println(" year of birth : ");
            Scanner info = new Scanner(System.in);
            while (!info.hasNext("[0-M]+")) {
            System.out.println("Enter number Please:");
            info.nextLine();
                            }
            year = info.nextInt();

        if(year > cd.get(Calendar.YEAR)){
            System.out.println("not born yet");
                    }

        while(year > cd.get(Calendar.YEAR)){
            year = Integer.parseInt(in.readLine());
          if(year > cd.get(Calendar.YEAR)){
         System.out.println("Invalid date of birth.");

        }
         }

    System.out.println(" month of birth  ");
        Scanner info2 = new Scanner(System.in);
        while (!info2.hasNext("[0-M]+")) {
        System.out.println("Please enter a number:");
        info.nextLine();
        }
        month = info2.nextInt();
    if(month < 1 || month > 12){

        System.out.println(" enter only  month between 1 to 12.");
    while(month < 1 || month > 12){
        month = Integer.parseInt(in.readLine());
    if(month < 1 || month > 12){
        System.out.println("only enter month between 1 to 12.");
                                        }
                                }
                            }

    else{
        month--;
    if(year == cd.get(Calendar.YEAR)){
    if(month > cd.get(Calendar.MONTH)){
            System.out.println("Invalid input!");
            System.exit(0);
             }
        }
    }

    System.out.println(" day of birth  ");
    Scanner info3 = new Scanner(System.in);
        while (!info3.hasNext("[0-M]+")) {
            System.out.println("Please enter a number:");
                info3.nextLine();
                        }
        day = info3.nextInt();
            if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11){
                if(day > 31 || day < 1){
                    System.out.println("Please enter day between 1 to 31.");
                while(day > 31 || day < 1){
                    day = Integer.parseInt(in.readLine());

                    if(day > 31 || day < 1){
                     System.out.println("Please enter day between 1 to 31.");
                            }
                        }
                    }
                }


            else if(month == 3 || month == 5 || month == 8 || month == 10){
                if(day > 30 || day < 1){
                    System.out.println("Please enter day between 1 to 30.");
                while(day > 30 || day < 1){
                    day = Integer.parseInt(in.readLine());
                        if(day > 30 || day < 1){
                         System.out.println("Please enter day between 1 to 30.");

                            }
                        }
                    }
                }


            else{
                if(new GregorianCalendar().isLeapYear(year)){
                    if(day < 1 || day > 29){
                        System.out.println("Please enter day between 1 to 29.");
                while(day < 1 || day > 29){
                    day = Integer.parseInt(in.readLine());
                        if(day < 1 || day > 29){
                         System.out.println("Please enter day between 1 to 29.");
                            }
                        }
                    }
                }

                else if(day < 1 || day > 28){
                    System.out.println("Please enter day between 1 to 28.");
                while(day < 1 || day > 28){
                    day = Integer.parseInt(in.readLine());
                        if(day < 1 || day > 28){
                            System.out.println("Please enter day between 1 to 28.");
                            }
                        }
                    }
                }


  if(year == cd.get(Calendar.YEAR)){
        if(month == cd.get(Calendar.MONTH)){
            if(day > cd.get(Calendar.DAY_OF_MONTH)){
                System.out.print("Invalid date!");
                System.exit(0);
                            }
                        }
                    }
                 }



  catch(FileNotFoundException e){

        System.out.print("Error opening the file" +
        fileName);
        System.exit(0);
                    }
  catch(NumberFormatException ne){
        System.out.println(ne.getMessage() + " is not a legal entry!");
        System.out.println("Please enter number.");
        System.exit(0);
                 }





  Calendar bd = new GregorianCalendar(year, month, day);

  ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
          if(cd.before(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
                    ageYears--;
                    ageMonths = (12 - (bd.get(Calendar.MONTH) + 1)) + (bd.get(Calendar.MONTH));
                if(day > cd.get(Calendar.DAY_OF_MONTH)){
                    ageDays = day - cd.get(Calendar.DAY_OF_MONTH);
                        }
                else if(day < cd.get(Calendar.DAY_OF_MONTH)){
                    ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
                    }
                else    {
                    ageDays = 0;
                    }
                 }
          else if(cd.after(new GregorianCalendar(cd.get(Calendar.YEAR), month, day))){
                ageMonths = (cd.get(Calendar.MONTH) - (bd.get(Calendar.MONTH)));
                if(day > cd.get(Calendar.DAY_OF_MONTH))
                    ageDays = day - cd.get(Calendar.DAY_OF_MONTH) - day;
                else if(day < cd.get(Calendar.DAY_OF_MONTH)){
                    ageDays = cd.get(Calendar.DAY_OF_MONTH) - day;
                        }
                else
                    ageDays = 0;
                }
          else      {
                ageYears = cd.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
                ageMonths = 0;
                ageDays = 0;
          }

        outputStream.println("\t\t\t\t\t" + ageYears + " years");



        outputStream.close();
        System.out.println("Those lines were written to " +fileName);
        ans--;


    System.out.println("Try again[1=NO, 2=YES]:");
    Scanner scan = new Scanner(System.in);
    while (!scan.hasNext("[1-2]")){
        System.out.println("Enter only 1 and 2");
        scan.nextLine();
    }

    int t = scan.nextInt();

    ans = t;

}

    while (ans == 2);


}

}

explain

this program will output first name ,last name, ang gender in the notepad..
and this output i am going to edit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.