Results 1 to 6 of 6

Thread: Help Plz

  1. #1
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default Help Plz

    I am trying to write a Server Client code,
    I did mange to connect both the server and client and exchange data.

    now in my server I created an ArrayList with three values, the client ask for a studentId, this ID is send to the server the serve looks the array liste if the ID found in array list, the server send the student Grade to the client,
    I did create the array list , but i dont know how to search in array list for Student Id, as I have three fileds, and then how to send this grade to the client,

    thanks for help in advance

    here is my server code.


    Java Code:
    package distributedsystem;
    
    /**
     *
     * @author Harris
     */
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.List;
    
    class Server
    {
    	public static void main(String args[])
    	{
    
    [COLOR="red"]      //Creating an ArrayList of Students
          ArrayList<Students> students = new ArrayList<Students>();
    
          students.add(new Students("David John", "w54321", "Grade A"));
          students.add(new Students("Jack Great", "w12345", "Grade C"));[/COLOR]
    
    		try  {
    
         ServerSocket sock = new ServerSocket(8187);  //Start server listening at port:8189
    
         String draw = null;
         String Aresult = null;
         String StudentID;
    
         while(true){		//Infinite loop starts here... Server never exits.
         Socket newsock = sock.accept();	//Listen for client... virtually execution gets locked here until some client calls the server.
    
         DataInputStream inp = new DataInputStream(newsock.getInputStream());	//Stream to recieve clients data.
         PrintStream outp = new PrintStream(newsock.getOutputStream());	//Stream to write data to client.
    
    
    		//Get client's move first. Read from client's stream.
                    StudentID = inp.readLine();
                    //Write to client's stream.
    		outp.println(StudentID);
                    
                         [COLOR="Red"] for (Students e : students)
                          {
                          if(StudentID.equalsIgnoreCase())
                          {
                              System.out.println("")
                          }
                            }[/COLOR]                      
                          System.out.println(StudentID);
                       
                            }
    
            } catch(Exception e){
    			System.out.println("I/O error");
    		}
            
            }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Surely the Students class (shouldn't that be Student?) has a method called getID() or getStudentID() or something similar. Why not call this on the Student object inside the for loop, "e", and use this to compare to the String entered by the user.

  3. #3
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default

    this is my Sudents Class

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package distributedsystem;
    
    
    
    /**
     *
     * @author Harris
     */
    public class Students {
    
       public Students(String n, String idee, String gr)
    
       {
          name = n;
          id = idee;
          grade = gr;
       }
    
       public String getName()
       {
          return name;
       }
    
       public String getId()
       {
          return id;
       }
    
       public String getGrade()
       {
          return grade;
       }
    
       private String name;
       private String id;
       private String grade;
    }
    I do have a methode called getID, can you be little bit more clear please or some code exmple?

    thanks

  4. #4
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default

    I write this way the code :

    for (Students e : students)
    {
    if(StudentID.equalsIgnoreCase(e.getId()))
    {
    System.out.println(e);
    }
    }

    but the server then shows this message for shoing "e"

    distributedsystem.Students@190d11

  5. #5
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default

    thanks god it :) now have to procced further :) to complete my project.

  6. #6
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default

    I manage the to search in Arraylist and then show the result in both server side and client side, but I am facing problem in how to show Student Grade in condation, i mean if the client request for an invalid Student ID, the server should send the client back a message that its invalid Student ID, here is my code,
    any suggestino please?

    Server Code

    Java Code:
     StudentID = inp.readLine();
                    //Write to client's stream.
    		//outp.println(StudentID);
                    System.out.println(StudentID);
                    
                          for (Students e : students)
                          {
                          if(e.getId().equalsIgnoreCase(StudentID))
                          {
                              System.out.println(e.getGrade());
                              outp.println(e.getGrade());break;
                          }else {
                              String invalidID=("invalid Student ID");
                              outp.println(invalidID);
                          }
                            }
    
                            }

    Client Code

    Java Code:
                        String Studentgrade = inp.readLine();
                        System.out.println("Grade for StudentID " + StudentID +" is : "+Studentgrade);
    
                        String Studentgrade1 = inp.readLine();
                        System.out.println(Studentgrade1);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •