Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Help In Java Programming (http://www.programmingforums.org/showthread.php?t=1243)

cpas Nov 21st, 2004 10:41 PM

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)

Eggbert Nov 22nd, 2004 8:33 AM

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;
}


cpas Nov 22nd, 2004 12:45 PM

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.

Ooble Nov 22nd, 2004 4:34 PM

What's the error message?

Eggbert Nov 23rd, 2004 3:57 PM

>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?


All times are GMT -5. The time now is 1:56 AM.

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