Results 1 to 6 of 6
Thread: Help Plz
- 02-22-2010, 12:04 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
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"); } } }
-
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.
- 02-22-2010, 12:28 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
this is my Sudents Class
I do have a methode called getID, can you be little bit more clear please or some code exmple?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; }
thanks
- 02-22-2010, 12:37 AM #4
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
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
- 02-22-2010, 12:41 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
thanks god it :) now have to procced further :) to complete my project.
- 02-22-2010, 10:56 AM #6
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
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);


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks