![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
This is my school assignment.Implement a Java program with the following screen outputs:Menu1) Add New Student Record2) Display All Student Records3) Enquire Student RecordI finished doing function 1 and 2, but for the third one: Enquire Student Record, I don't know how to write search function, can anyone write for me a simple program to search Student record? Thanks.
This is my program with first two function: import java.io.*;
class student
{
String studentID;
String Name;
String Address;
String Telephone;
public void setData() throws IOException
{//to set data of each student
BufferedReader stuData = new BufferedReader
(new InputStreamReader (System.in));
System.out.print("\n\t\tEnter StudentID: ");
studentID = stuData.readLine();
System.out.print("\n\t\tEnter Name of the Student: ");
Name = stuData.readLine();
System.out.print("\n\t\tEnter Address: ");
Address = stuData.readLine();
System.out.print("\n\t\tEnter Telephone number: ");
Telephone = stuData.readLine();
}
public void Display ()
{
//display detail of each student
System.out.println();
System.out.print (studentID + "\t\t"+Name + "\t" +Address+ "\t\t" +Telephone);
}
}//end of student class
class StudentProgram
{
//student_count gives Number of student records in Array.
static int student_count = 0;
/*studnetRec is an Array of student objects which can store
up to the mazimum of 20 student objects*/
static student studentRec[] = new student[20];
public static void clear_screen()
//to clear screen
{for (int i=0; i<5; i++)
System.out.println();
}
public static void Add_Rec() throws IOException
{
clear_screen();
studentRec[student_count] = new student();
//calling setdata to set data
studentRec[student_count].setData();
//To count student record just added
student_count++;
//to display main menu
Menu();
}
public static void View_All() throws IOException
{
clear_screen();
/*creation of student object uf the
user choose choice 1 to add
new Student Record*/
BufferedReader ch = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("StudID \t\tName \t\tAddress \t\t\tTel");
//to display all student records in Array
for (int i=0; i <student_count; i++)
studentRec [i].Display();
System.out.println("\nPress Any key to display menu ");
String anykey = ch.readLine();
}
public static void Menu() throws IOException
{
int choice;
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
do{
clear_screen();
for (int i=0; i<4; i++)//i<4 (no. of lines from the top)
System.out.println();
System.out.println("\t\t\t###################################");
System.out.println("\n\t \t\t(1) Add New Student Record");
System.out.println("\n\t \t\t(2) Display All Student Records");
System.out.println("\n\t \t\t(3) Enquire Student Record");
System.out.println("\n\t \t\t(4) Exit Program");
System.out.println("\t\t\t###################################");
System.out.print("Enter Your Choice");
choice = Integer.parseInt(br.readLine());
//to read any data from the keyboard(readline)
switch(choice)
{
case 1:
Add_Rec();
break;
case 2:
View_All();
break;
case 3:
System.out.println("You choose enquire student record");
break;
}
}while(choice!=4);
System.out.println("\n\t\t\tThank you for using Program");//n\t\t(alignment)
}
public static void main (String args[]) throws IOException
{
Menu();
}
}//end of classI WILL BE GREATFUL WITH YOUR HELP. (You have been [code] tagged and slightly formatted! - SykkN) |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
The simplest search method is called sequential search. You simply walk along the array and test for a match. If you reach the end of the array then the search fails:
// Search the student list using Name as the key
public student sequential_search ( student[] list, int size, String key ) {
for ( int i = 0; i < size; i++ ) {
if ( list[i].Name.equals ( key ) )
return list[i];
}
return null;
}// Search the student list using Name as the key
public student sequential_search ( student[] list, int size, String key ) {
while ( --size >= 0 ) {
if ( list[size].Name.equals ( key ) )
return list[size];
}
return null;
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
Thanks for your help, but I put your codes into my program but keep one have error message, Sorry, but if it is possible help me to put your program into my program too?
Sorry to give you trouble. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
What's the error message?
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>but I put your codes into my program but keep one have error message
You could try to integrate my code into your program, but the intention was to provide an understandable example of the algorithm which you could use as a template for your own function. What error are you getting? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|