I NEED A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX,
if we have an DB file has a records of students number and names
like this
(Student file)
stNum**stName
1 jack
2 maya
3 sam
4 alex
5 jane
. .
. .
. .
this file may have a thousands records, if we want to search for
a specific record(student),
we need to find it by (Index file), in this index file we have
this
(Index file)
stNum**Address
1 (address or the record num from the Student file)
. .
. .
. .
so here we need to write a program with JAVA
to make it easy to find this specific record.
Implementation in JAVA:
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Student_Index {
public static void main(String[] args) throws
FileNotFoundException {
// Student file class and its
address
File stufile = new
File("C:\\Users\\Ankit\\Desktop\\sname.txt");
// creating Student name's Scanner
class object
Scanner ss = new
Scanner(stufile);
//
Address file class and its address
File addfile = new
File("C:\\Users\\Ankit\\Desktop\\address.txt");
// creating
Student Address's Scanner class object
Scanner sa = new
Scanner(addfile);
// list of type Students
ArrayList<Students> list= new ArrayList<>();
// read file and
Store in Arraylist
try
{
while(
ss.hasNext() && sa.hasNext() )
{
String name=ss.next();
String add=sa.next();
Students s = new Students(name,add);
list.add(s);
}
}
finally
{
ss.close();
sa.close();
}
// print
System.out.println(" Print All The Students Details\n");
System.out.println(" INDEX NAME
ADDRESS ");
System.out.println(" ");
for(int i=0;i<list.size();i++)
{
Students st =
list.get(i);
System.out.println(" "+(i+1)+" "+st.getname()+"
"+st.getaddrs());
}
// you can simply get details of any
particular student just by using index
// like that, now i am printing
Student at index 3
System.out.println();
System.out.println("Printing
details of Student at index 3 : ");
Students s= list.get((3-1));
System.out.println("Index : 3, Name
: "+s.getname()+", Address : "+s.getaddrs() );
System.out.println();
}
}
// class Students
class Students
{
// instance Variables
String name;
String addrs;
// Argumented constructor
public Students(String name,String addrs){
this.name=name;
this.addrs=addrs;
}
// Getters and Setters methods
public String getname()
{
return name;
}
public void setname()
{
this.name= name;
}
public String getaddrs()
{
return addrs;
}
public void setaddrs(String addrs)
{
this.addrs= addrs;
}
}
SAMPLE OUTPUT:
FILES:
1. sname.txt
2.address.txt
// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in
commnets
// THANK YOU:-)
Get Answers For Free
Most questions answered within 1 hours.