Tuesday, June 25

Program for Arranging ascending order of names in the given Text file using java

Hi All,

Here I write a program for Arranging ascending order of names in the given file.

 1. Read names from the given file.  

2. Checking name contains initial then the initial to be replaces it and end of the name.

3. Very easy method to list names in ascending order within seconds.

4. Again, I write values of out put in separate file.


Note:
1. Each line contains a single name in the given file.
2. The empty line between the name a not allowed. It considers as an end of the file.  

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class attendaceOrder {

    public static void main(String[] args) throws IOException {
        BufferedReader br = null;
        ArrayList<String> unsortedList = new ArrayList<String>();   
        File file;
        /* File Read Method*/
        try {

            String name;

            br = new BufferedReader(new FileReader("C:\\Documents and Settings\\sify\\Desktop\\names.txt"));

            while ((name = br.readLine()) != null) {
                System.out.println("Users Listed in the sheet :\n" + name);
                /* Check the .(dot) pattern in given names*/
                String dot = ".";
                boolean res = name.contains(dot);
                if (res == true) {
                    int l = name.lastIndexOf(dot);
                    if (l == 1 || l == 3 || l == 5) {
                        String Lastname = name.substring(0, l);
                        int name1 = name.substring(l + 1).length();
                        String Firstname = name.substring(l + 1);
                        String str = Firstname.concat("." + Lastname);
                        unsortedList.add(str);

                    } else {
                        //
                    }
                } else {
                    unsortedList.add(name);
                }
            }
            /* Adding the values in ArrayList*/
            Collections.sort(unsortedList);
            System.out.println("Users Listed in the sheet - Ascending order :\n " + unsortedList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        Arrays.sort(unsortedList.toString().toCharArray());

        /* File Write Method*/
        try {
            file = new File("C:\\Documents and Settings\\sify\\Desktop\\names1.txt");
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            for (String s : unsortedList) {
                System.out.println(s);
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            /* Write list of names in given file*/
            bw.write(unsortedList.toString());
            bw.close();

            System.out.println("Done :-)");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

OUTPUT

~~~~~~~~~

Users Listed in the sheet :    Aswini
Users Listed in the sheet :    Asvini
Users Listed in the sheet :    Bharani
Users Listed in the sheet :    Parani
Users Listed in the sheet :    Krithika
Users Listed in the sheet :    Karthigai
Users Listed in the sheet :    Rohini R
Users Listed in the sheet :    Rogini A
Users Listed in the sheet :    Mrigashiras
Users Listed in the sheet :    Mirugasirisham
Users Listed in the sheet :    Aardhra
Users Listed in the sheet :    Arudra
Users Listed in the sheet :    Thiruvadhirai
Users Listed in the sheet :    Punarvasu
Users Listed in the sheet :    Punarpoosam
Users Listed in the sheet :    Pushyami
Users Listed in the sheet :    Poosam
Users Listed in the sheet :    Ashlesha
Users Listed in the sheet :    Ayilyam
Users Listed in the sheet :    Magha
Users Listed in the sheet :    Makha
Users Listed in the sheet :    Magam
Users Listed in the sheet :    P.Phalguni
Users Listed in the sheet :    Poorva Phalguni
Users Listed in the sheet :    Pubba
Users Listed in the sheet :    Pooram
Users Listed in the sheet :    U.Phalguni
Users Listed in the sheet :    Uthraphalguni
Users Listed in the sheet :    Uttara
Users Listed in the sheet :    Uthiram
Users Listed in the sheet :    Haatha
Users Listed in the sheet :    Haa tham
Users Listed in the sheet :    Chitra
Users Listed in the sheet :    Chithirai
Users Listed in the sheet :    Swaathi
Users Listed in the sheet :    Swathi
Users Listed in the sheet :    Vishaakha
Users Listed in the sheet :    Visakam
Users Listed in the sheet :    Anuraadha
Users Listed in the sheet :    Anusham
Users Listed in the sheet :    Jyeshta
Users Listed in the sheet :    Kettai
Users Listed in the sheet :    Moola
Users Listed in the sheet :    Moolam
Users Listed in the sheet :    P.Shada
Users Listed in the sheet :    Poorvashaada
Users Listed in the sheet :    Pooradam
Users Listed in the sheet :    U.Shada
Users Listed in the sheet :    Uthrashaada
Users Listed in the sheet :    Uthiradam
Users Listed in the sheet :    Shraavan
Users Listed in the sheet :    Thiruvonam
Users Listed in the sheet :    Dhanishta
Users Listed in the sheet :    Avittam
Users Listed in the sheet :    Shathabhisha
Users Listed in the sheet :    Sathayam
Users Listed in the sheet :    P.Bhadra
Users Listed in the sheet :    Poorvabhadra
Users Listed in the sheet :    Poorattathi
Users Listed in the sheet :    U.Bhadra
Users Listed in the sheet :    Uthrabhadra
Users Listed in the sheet :    Uthirattathi
Users Listed in the sheet :    Revathi s
Users Listed in the sheet :    Revathi a
Users Listed in the sheet :    Hari Prasoon
Users Listed in the sheet :    Bala murugan
Users Listed in the sheet :    Hari prasad


Users Listed in the sheet - Ascending order :
 [Aardhra, Anuraadha, Anusham, Arudra, Ashlesha, Asvini, Aswini, Avittam, Ayilyam, Bala murugan, Bhadra.P, Bhadra.U, Bharani, Chithirai, Chitra, Dhanishta, Haa tham, Haatha, Hari Prasoon, Hari prasad, Jyeshta, Karthigai, Kettai, Krithika, Magam, Magha, Makha, Mirugasirisham, Moola, Moolam, Mrigashiras, Parani, Phalguni.P, Phalguni.U, Pooradam, Pooram, Poorattathi, Poorva Phalguni, Poorvabhadra, Poorvashaada, Poosam, Pubba, Punarpoosam, Punarvasu, Pushyami, Revathi a, Revathi s, Rogini A, Rohini R, Sathayam, Shada.P, Shada.U, Shathabhisha, Shraavan, Swaathi, Swathi, Thiruvadhirai, Thiruvonam, Uthiradam, Uthiram, Uthirattathi, Uthrabhadra, Uthraphalguni, Uthrashaada, Uttara, Visakam, Vishaakha]


Done :-)


No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...