Try out these Programs
(a)
package pack1;
import java.util.*;
import java.lang.*;
public static void main(String args[]){
String name = "";
ArrayList list=new ArrayList();
Orderly orderly = new Orderly ();
try
{
list=orderly.displayRecordNew();
Collections.sort(list);
/*
Sorts the array list
If We have to Sort the Name in Descending Order then we have to uncomment the
below commented lines.
Comparator r = Collections.reverseOrder();
Collections.sort(list,r);
*/
Iterator l1 = list.iterator();
while(l1.hasNext()){
orderly = (Orderly)l1.next();
name = orderly.getOrderlyName();
System.out.println("Name is " + name);
}
}
catch(Exception e)
{
}
}
(b)
package pack1;
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
private String name;
public void setOrderlyName(String name){
this.name = name;
}
public String getOrderlyName(){
return name;
}
//Overriding the compareTo method
public int compareTo(Orderly o){
return (this.name).compareTo(o.name);
}
public ArrayList displayRecordNew() throws Exception
{
File file = new File("C://Users//Radio1//Desktop//Names.txt");
FileInputStream fis = null;
StringTokenizer st;
ArrayList showRecord=null;
try {
showRecord=new ArrayList();
fis = new FileInputStream(file);
/*
FileInputReader since we have to sort out the names in ascending order from the file Names.txt
*/
BufferedReader d = new BufferedReader(new InputStreamReader(fis));
String readingFromFile = "";
String intoArrayList = "";
while((readingFromFile = d.readLine()) != null)
{
st = new StringTokenizer(readingFromFile, ",");
while (st.hasMoreTokens())
{
Orderly orderly=new Orderly();
intoArrayList = st.nextToken();
orderly.setOrderlyName(intoArrayList);
showRecord.add(orderly);
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return showRecord;
}
(a)
package pack1;
import java.util.*;
import java.lang.*;
import pack1.Orderly;
public class Example{public static void main(String args[]){
String name = "";
ArrayList list=new ArrayList();
Orderly orderly = new Orderly ();
try
{
list=orderly.displayRecordNew();
Collections.sort(list);
/*
Sorts the array list
If We have to Sort the Name in Descending Order then we have to uncomment the
below commented lines.
Comparator r = Collections.reverseOrder();
Collections.sort(list,r);
*/
Iterator l1 = list.iterator();
while(l1.hasNext()){
orderly = (Orderly)l1.next();
name = orderly.getOrderlyName();
System.out.println("Name is " + name);
}
}
catch(Exception e)
{
}
}
}
(b)
package pack1;
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
class Orderly implements Comparable<Orderly>{
public String nameDesig = "a";private String name;
public void setOrderlyName(String name){
this.name = name;
}
public String getOrderlyName(){
return name;
}
//Overriding the compareTo method
public int compareTo(Orderly o){
return (this.name).compareTo(o.name);
}
public ArrayList displayRecordNew() throws Exception
{
File file = new File("C://Users//Radio1//Desktop//Names.txt");
FileInputStream fis = null;
StringTokenizer st;
ArrayList showRecord=null;
try {
showRecord=new ArrayList();
fis = new FileInputStream(file);
/*
FileInputReader since we have to sort out the names in ascending order from the file Names.txt
*/
BufferedReader d = new BufferedReader(new InputStreamReader(fis));
String readingFromFile = "";
String intoArrayList = "";
while((readingFromFile = d.readLine()) != null)
{
/*
Now We are using StringTokenizer to read the names from the FileList since the Names are separated using comma in the File and the resultant one by one we will store it in the ArrayList which we will sort it out using Collections.sort(list);
Since this class (Orderly) is imported in the class Example it will automatically sort out the List and we will get the Names in Ascending Order on further iteration of the List
*/st = new StringTokenizer(readingFromFile, ",");
while (st.hasMoreTokens())
{
Orderly orderly=new Orderly();
intoArrayList = st.nextToken();
orderly.setOrderlyName(intoArrayList);
showRecord.add(orderly);
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return showRecord;
}
}
(c)Names.txt will have the following contents
Seshadri,Lakshmi,Padma,Balaji,Koushick,Hari,Sudarshan,Anirudh,Sriram,Rajagopalan,Mahesh,Zombia,Karthick,Raghavendra,Mahati,Kamran
(d)Now on running the Example.java file we get the following as Result(Sorted Out)
Name is Anirudh
Name is Balaji
Name is Hari
Name is Kamran
Name is Karthick
Name is Koushick
Name is Lakshmi
Name is Mahati
Name is Mahesh
Name is Padma
Name is Raghavendra
Name is Rajagopalan
Name is Seshadri
Name is Sriram
Name is Sudarshan
Name is Zombia
No comments:
Post a Comment