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

    Default If statment not working:(

    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
    Client Code


    Java Code:
               String Studentgrade = inp.readLine(); // this it to recive the grade for requested StudentId from server
    
                        System.out.println("Grade for StudentID " + StudentID +" is : "+Studentgrade);
    
                        String Studentgrade1 = inp.readLine(); // this is to revied the message StudentID not found, from server
                        System.out.println(Studentgrade1);

  2. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    your for-each loop needs to be changed to a for loop:
    Java Code:
    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);
                          }
                            }
    try instead something like this
    Java Code:
    boolean found = false;
    for(int i = 0; i<students.size() && !found; i++)
    {
        if(students.get(i).getId().equalsIgnoreCase(StudentID))
                      found = true;    
    }
    if found is false after running the for-loop, then the entered student id is not in the list..

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

    Default

    Do you mean this way?

    Java Code:
                    boolean found = false;
                    for(int i = 0; i<students.size() && !found; i++)
                    {
                        if(students.get(i).getId().equalsIgnoreCase(StudentID)){
                      found = true;
                      String grade = ("Grade for StudentID " + StudentID +" is : "+students.get(i).getGrade());
                      outp.println(grade);}
                        else{
                             String invalidID=("invalid Student ID");
                             outp.println(invalidID);
                        }
    
                        
                    }
    the problem in this case is that how to send only one stream from for loop to the client, I mean if the StudentId is Valid so the server send its grade if not then it sends that the studentID is invalid,

    this is my server and client complete code.

    Server Code

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.List;
    
    class Server
    {
    	public static void main(String args[])
    	{
    
          //Creating an ArrayList of Students
          ArrayList<Students> students = new ArrayList<Students>();
          students.add(new Students("Harris Bahazada", "w55555", "Grade A"));
          students.add(new Students("Shakib Ahmad", "w12212", "Grade B"));
          students.add(new Students("David John", "w54321", "Grade C"));
    
    		try  {
    
         ServerSocket sock = new ServerSocket(8187);  //Start server listening at port:8189
    
         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);
                    System.out.println(StudentID);
    
    
                    boolean found = false;
                    for(int i = 0; i<students.size() && !found; i++)
                    {
                        if(students.get(i).getId().equalsIgnoreCase(StudentID)){
                      found = true;
                      String grade = ("Grade for StudentID " + StudentID +" is : "+students.get(i).getGrade());
                      outp.println(grade);}
                        else{
                             String invalidID=("invalid Student ID");
                             outp.println(invalidID);
                        }
    
                        
                    }
     }
    
            } catch(Exception e){
    			System.out.println("I/O error");
    		}
            
            }
    }
    Client Code

    Java Code:
    package distributedsystem;
    
    /**
     *
     * @author Harris
     */
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class Client
    {
    
       public static void main(String[] args)
       {
    
          try
          {
             //Creating a new cokkect called s, and it will connecte to localhost port 8187
             Socket s = new Socket("localhost", 8187);
    
             try
             {
               
                PrintWriter out = new PrintWriter(s.getOutputStream(),true);
                DataInputStream inp = new DataInputStream(s.getInputStream());
    
                        //asking for Student ID
                        System.out.println("Enter Student ID");
                        //Scanning StudentID
                        Scanner IDScanner = new Scanner(System.in);
                        String StudentID  = IDScanner.nextLine();
                        //sending StudentID to the Server
                        out.println(StudentID);
                        //recievning student result from the server and print it on client screen.
                        String Studentgrade = inp.readLine();
                        System.out.println(Studentgrade);
    
    
                        String InvalidStudentID = inp.readLine();
                        System.out.println(InvalidStudentID);
    
    
             }
             finally
             {
               s.close();
             }
          }
          catch (IOException e)
          {
             e.printStackTrace();
          }
       }
    }

  4. #4
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    remove:
    else{
    String invalidID=("invalid Student ID");
    outp.println(invalidID);
    }

    if found is false after running the for-loop, then the studentID is invalid
    if(!found)
    { String invalidID=("invalid Student ID");
    outp.println(invalidID);}

    I know this only solve part of your program.....
    Last edited by wtd_nielsen; 02-23-2010 at 08:24 PM.

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

    Default

    yeah, this is only part of it, but how can the server send the Student grade in case of valid StudentID?

  6. #6
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    I guess something like this:


    if(found)
    outp.println("Valid")
    else
    outp.println("Not Valid")

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

    Default

    Thanks alot its working like a charm, now I have to put everything in order and in good comment and then start to add thread in my server.

    Thanks

Similar Threads

  1. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  2. statment replay!!!
    By moamen in forum New To Java
    Replies: 4
    Last Post: 09-25-2009, 12:49 PM
  3. Issue using FOR statment with NetBeans
    By Deathmonger in forum Advanced Java
    Replies: 3
    Last Post: 07-30-2008, 12:46 AM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

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