Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 21st, 2004, 9:41 PM   #1
cpas
Newbie
 
Join Date: Nov 2004
Posts: 3
Rep Power: 0 cpas is on a distinguished road
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 class


I WILL BE GREATFUL WITH YOUR HELP.

(You have been [code] tagged and slightly formatted! - SykkN)
cpas is offline   Reply With Quote
Old Nov 22nd, 2004, 7:33 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
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;
}
An alternative method that removes the index variable takes advantage of any lack of ordering in the list:
// 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;
}
Eggbert is offline   Reply With Quote
Old Nov 22nd, 2004, 11:45 AM   #3
cpas
Newbie
 
Join Date: Nov 2004
Posts: 3
Rep Power: 0 cpas is on a distinguished road
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.
cpas is offline   Reply With Quote
Old Nov 22nd, 2004, 3:34 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
What's the error message?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 23rd, 2004, 2:57 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>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?
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC