Solved

Java Simple Client/Server

Posted on 2007-11-14
24
345 Views
Last Modified: 2010-03-30
I have set up a simple java/client server program though I have declared within my code the address of the remote server. How do i alter this code so that the client will be asked of the address to which he would like to connect. I've declared the address as localhost.

Server Application###########

import java.io.*;
import java.net.*;

public class DVDServerApp {

    public static void main(String[] args) throws IOException {

        int port = 4445;
       
        // Set up the 'server socket' connection
        ServerSocket sSocket = null;
        try {
            sSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.out.println("Couldn't listen on port: " + port + "; " + e);
            System.exit(1);
        }
       
        while(true) {
            System.out.println("Waiting for client...");
           
            // Set up the client socket
            Socket client;
            client = sSocket.accept();
            System.out.println("Accepted from " + client.getInetAddress());
           
            // Initiate a server thread and start it
            DVDserver c = new DVDserver(client);           #####Problem here?
            c.start();
        }
    } // main
   
} // DVDServerApp

Server############

import java.io.*;
import java.net.*;

public class DVDserver extends Thread {

    protected Admin myadmin;
    protected Socket s;

   
    DVDserver(Socket theSocket, Admin myadmin) {
        System.out.println("New client.");
        this.myadmin = Admin;                                #########Problem here?
        s = theSocket;
    }
   
    public void run() {

        PrintWriter toClient = null;
        BufferedReader fromClient = null;
        try {
            // Set up the input / output streams for the server
            toClient = new PrintWriter(s.getOutputStream(), true);
            fromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
                                       
            String inputLine; // holds line from client
            String surname, forename, username;
            int initialBalance, accNum, balance, amount;
           
            // Initiate conversation with client
            while ((inputLine = fromClient.readLine()) != null) {
                if (inputLine.equalsIgnoreCase("A")) {
                    System.out.println("New admin");
                    forename= fromClient.readLine();
                    surname = fromClient.readLine();
                    username = fromClient.readLine();
                    inputLine = fromClient.readLine(); // read initial balance as string
                    initialBalance = Integer.parseInt(inputLine);
                    accNum = myadmin.newAccount(surname, forename, username, initialBalance);
                    toClient.println("admin Number: " + accNum);
                   
                } else if (inputLine.equalsIgnoreCase("B")) {
                    System.out.println("Enter in admin No.");
                    accNum = Integer.parseInt(fromClient.readLine());
                    forename = myadmin.getSurname(accNum);
                    toClient.println("Forename: "+ forename);
                    surname = myadmin.getForename(accNum);
                    toClient.println("Surname: "+ surname);
                    username = myadmin.getUsername(accNum);
                    toClient.println("User Name: "+ username);
                    balance= myadmin.getBalance(accNum);
                    toClient.println("Oustanding Balance: " + balance);
                }
                    else if (inputLine.equalsIgnoreCase("C")) {
                        System.out.println("Outstanding Balance: deduct");
                        accNum = Integer.parseInt(fromClient.readLine());
                        amount = Integer.parseInt(fromClient.readLine());
                        balance= myadmin.debit(accNum,amount);
                        toClient.println(balance); //sends the new balance after money was taken off
                    }
                    else if (inputLine.equalsIgnoreCase("D")) {
                        System.out.println("Outstanding Balance: credit");
                        accNum = Integer.parseInt(fromClient.readLine());
                        amount = Integer.parseInt(fromClient.readLine());
                        balance= myadmin.credit(accNum,amount);
                        toClient.println(balance); //sends the new balance after money was taken off
                    }else {
                System.out.println("Unknown command");
            }
            if (inputLine.equals("exit"))
                break;
            }
                // Close all streams
                System.out.println("Closing Echo Server...");
                toClient.close();
                fromClient.close();
                s.close();
               
            } catch (IOException ioe) {
                System.out.println("IO Exception: " + ioe);
                System.exit(1);
            }
        } // run
       
    } // DVDserver

Client###########
import java.io.*;
import java.net.*;

public class DVDClient {

    public static void main(String[] args) throws IOException {

        Socket serverSocket = null;
        PrintWriter toServer = null;
        BufferedReader fromServer = null;
        String remoteServer = "localhost";
         
        try {
            // Try connecting to the 'remote' machine on port 4445
            serverSocket = new Socket(remoteServer, 4445);
            toServer = new PrintWriter(serverSocket.getOutputStream(), true);
            fromServer = new BufferedReader(new InputStreamReader(
                                        serverSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + remoteServer);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: " + remoteServer);
            System.exit(1);
        } // try-catch

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
                                     

        String userInput, forename, surname, username, balance, reply, accNum, amount;

   
        do {
              System.out.println("Please select one of the following options");
              System.out.println("a. Add new renter account");
              System.out.println("b. Retrieve a renters details");
              System.out.println("c. Deduct oustanding balance of a renter");
              System.out.println("d. Add to a renters oustanding balance");             
              System.out.println("e. List all the DVD's");
              System.out.print("Option: ");
            userInput = stdIn.readLine();
           
            if (userInput.equalsIgnoreCase("A")) {
                System.out.print("Forename:  ");
                forename = stdIn.readLine();
                System.out.print("Surname:  ");
                surname = stdIn.readLine();
                System.out.print("username:  ");
                username = stdIn.readLine();
                System.out.print("Set outstanding balance if any: ");
                balance = stdIn.readLine();
                toServer.println(userInput);
                toServer.println(forename);
                toServer.println(surname);
                toServer.println(username);
                toServer.println(balance);
                reply = fromServer.readLine();
                System.out.println("Reply: " + reply);
            }
                else if (userInput.equalsIgnoreCase("B")) {
                    System.out.print("Renters Account Number: ");
                    accNum = stdIn.readLine();
                    toServer.println(userInput);
                    toServer.println(accNum);
                    reply = fromServer.readLine();
                    System.out.println(reply); //forename
                    reply = fromServer.readLine();
                    System.out.println(reply); //surname
                        reply = fromServer.readLine();
                        System.out.println(reply); //username
                          reply = fromServer.readLine();
                          System.out.println(reply); } // outstanding balance
           
                else if (userInput.equalsIgnoreCase("C")) {
                    System.out.print("Renters Account Number:  ");
                    accNum = stdIn.readLine();
                    System.out.print("Amount to be deducted: ");
                    amount = stdIn.readLine();
                    toServer.println(userInput);
                    toServer.println(accNum);
                    toServer.println(amount);
                    reply = fromServer.readLine();
                    System.out.println("New oustanding balance stands at " + reply);
                    System.out.println("");
                   
            }                
                else if (userInput.equalsIgnoreCase("D")) {
                System.out.print("Renters Account Number:  ");
                accNum = stdIn.readLine();
                System.out.print("Amount to be credited: ");
                amount = stdIn.readLine();
                toServer.println(userInput);
                toServer.println(accNum);
                toServer.println(amount);
                reply = fromServer.readLine();
                System.out.println("New oustanding balance stands at " + reply);
                System.out.println("");
        }      
               
 else {
               
            }
        } while (!userInput.equals("Exit"));
   
        toServer.close();
        fromServer.close();
        stdIn.close();
        serverSocket.close();
    } // main
   
} // DVDClient

There are a few problems aswell as I noted. Any help anyone. Thanks!
Here are my classes if they help saved within Admin.java

Admin.java############
public class Admin
{

      // DECLARING THE USER ACCOUNT VARIABLES
      private static final int MAX_ACCOUNTS = 100;
      private int currentTotal = 0;
      private int currentAccNum = 1;

      Renter renters[] = new Renter[MAX_ACCOUNTS];

      // declaring our DVD VARIABLES
      private final int MAX_DVD = 1000;
      private int currentDVDTotal = 0;
      private int currentUniqueID = 1;

      DVD DVDs[] = new DVD[MAX_DVD];

      public synchronized int newAccount(String forename, String surname,
              String username, int initialBalance)
      {
            // Generates a new account and returns the account
            // number if successful, otherwise returns 0.
            if (currentTotal < MAX_ACCOUNTS)
            {
                  currentAccNum++;
                  renters[currentTotal++] = new Renter(forename, surname, username,
                          initialBalance, currentAccNum);
                  return currentAccNum;
            }
            else
            {
                  return 0;
            }
      } // newAccount

      public synchronized int newDVD(String title, String classification,
              String leadingCharacter, String director, boolean available,
              int price)
      {
            // Generates a new account and returns the account
            // number if successful, otherwise returns 0.
            if (currentDVDTotal < MAX_DVD)
            {
                  currentUniqueID++;

                  DVDs[currentDVDTotal++] = new DVD(title, classification,
                          leadingCharacter, director, available, price,
                          currentUniqueID);
                  return currentUniqueID;
            }
            else
            {
                  return 0;
            }
      } // newAccount

      public synchronized int getBalance(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getBalance();
            }
            else
            {
                  return 0;
            }
      } // getBalance

      public synchronized String getForename(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getForename();
            }
            else
            {
                  return "";
            }
      } // getforename

      public synchronized String getSurname(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getSurname();
            }
            else
            {
                  return "";
            }
      } // getSurname

      public synchronized String getUsername(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getUsername();
            }
            else
            {
                  return "";
            }
      } // getSurname

      public synchronized int credit(int Renter, int amount)
      {
            Renter acc;
            acc = findAcc(Renter);
            if (acc != null)
            {
                  acc.credit(amount);
                  return getBalance(Renter);
            }
            else
            {
                  return 0;
            }
      } // deposit

      public synchronized int debit(int Renter, int amount)
      {
            Renter acc;
            acc = findAcc(Renter);
            if (acc != null)
            {
                  acc.debit(amount);
                  return getBalance(Renter);
            }
            else
            {
                  return 0;
            }
      } // debit

      public synchronized String convertToString()
      {
            String str = "";

            for (int i = 0; i < currentTotal; i++)
            {
                  str = str + renters[i].convertToString() + "\n";
            }
            return str;
      } // convertToString

      private synchronized Renter findAcc(int accountNum)
      {
            for (int i = 0; i < currentTotal; i++)
            {
                  if (renters[i].getAccountNum() == accountNum)
                        return renters[i];
            }
            return null;
      }

      // Initialisation block - create a few accounts
      public static void main(String[] args)
      {
            Admin a = new Admin();
            a.newAccount("Ali", "Shaw", "ashaw", 100);
            a.newAccount("Aaron", "Boyle", "aboyle", 200);
            a.newAccount("Cilla", "Black", "cblack", 10);
            a.newAccount("Hugh", "Brown", "hBrown", 50);
            a.newAccount("Barry", "White", "BWhite", 80);
      } // Initialisation block

} // admin

class Renter
{

      private String forename;
      private String surname;
      private String username;
      private int balance;
      private int accountNum;

      Renter(String forename, String surname, String username,
              int initialBalance, int accountNum)
      {

            forename = forename;
            surname = surname;
            username = username;
            balance = initialBalance;
            this.accountNum = accountNum;
      } // Renter

      public int getBalance()
      {
            return balance;
      }

      public int getAccountNum()
      {
            return accountNum;
      }

      public void credit(int amount)
      {
            balance += amount;
      }

      public void debit(int amount)
      {
            balance -= amount;
      }

      public String getForename()
      {
            return forename;
      }

      public String getSurname()
      {
            return surname;
      }

      public String getUsername()
      {
            return forename;
      }

      public String convertToString()
      {
            return (forename + "\t" + surname + "\t" + username + "\t" + accountNum
                    + "\t" + balance);
      }

} // Renter

class DVD
{

      private String title;
      private String classification;
      private String leadingCharacter;
      private String director;
      private boolean available;
      private int price;
      private int uniqueID;

      DVD(String title, String classification, String leadingCharacter,
              String director, boolean available, int price, int uniqueID)
      {

            title = title;
            classification = classification;
            leadingCharacter = leadingCharacter;
            director = director;
            price = price;
            available = available;
            this.uniqueID = uniqueID;
      } // Renter

      public int getUniqueID()
      {
            return uniqueID;
      }

      public void credit(int amount)
      {
            price += amount;
      }

      public void debit(int amount)
      {
            price -= amount;
      }

      public String getTitle()
      {
            return (title);
      }

} // dvd
0
Comment
Question by:bigbong00
  • 10
  • 9
  • 4
  • +1
24 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Interactively? Use JOptionPane to get String input.

;JOOP!
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
Comment Utility
Right now as both Client and server are running on the same system.
The  String remoteServer = "localhost"; is working.

If the client is running in some other machine then he has to use your IP address to connect to the remote server.

To know your IP address open command prompt and type ipconfig it will display you IP address.

then use this IP address in the  client program as below.

 String remoteServer = "xxx.xx.xx.xxx"; //ex: 172.16.0.131  replace xxx with your IP address.
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
how do you start your DVDServer?
0
 

Author Comment

by:bigbong00
Comment Utility
Yeah I knew how to do that but I would like the client to be able to state the ip adress each and every time the client is run through a command line (possibly using the try catch) but I don't know how to do it.

I've also marked within the server and server app where problems lay in the code. Do you know how to fix these errors?
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
ignore my previous post
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
Comment Utility
Sorry I think I got your question wrongly :) Yes as sciuriware: suggested you can use a nice Swing User Interface for taking details from Client.

Some thing interactive like these

http://www.java2s.com/Code/Java/Swing-JFC/Dialog.htm
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
// Initiate a server thread and start it
            DVDserver c = new DVDserver(client);           #####Problem here?
     
you wil get a compile error at the above linbe as there is no such constructor in  DVDServer

    DVDserver(Socket theSocket, Admin myadmin) {
        System.out.println("New client.");
        this.myadmin = Admin;                                #########Problem here?
        s = theSocket;
    }

you will get a compile error here as there is no variable called Admin

change the 2 lines like

DVDserver c = new DVDserver(client, new Admin());


this.myadmin = myadmin ;  
 
0
 

Author Comment

by:bigbong00
Comment Utility
I just start the server through the command prompt.

I dont want to use swing
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
also change you constructor of you DVD class like this


DVD(String title, String classification, String leadingCharacter,
              String director, boolean available, int price, int uniqueID)
      {

            this.title = title;
            this.classification = classification;
            this.leadingCharacter = leadingCharacter;
            this.director = director;
            this.price = price;
            this.available = available;
            this.uniqueID = uniqueID;
      } // Renter
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
Comment Utility
Then the other option is to take the remote server address as a Command line parameter and assign to

String remoteServer = args[0]; // Check for null before this.
0
 

Author Comment

by:bigbong00
Comment Utility
It is compiling now but the options are not working. I already have 5 accounts set up but when I create a new account it is assigning that account with the value 2
0
 

Author Comment

by:bigbong00
Comment Utility
DVD constructor changed
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:bigbong00
Comment Utility
Its is clearly not seeing the accounts I have created now following the changes I have made. getName, getForename and getUserName are all returning null values. Why?

and in reply to.....

shivaspk:
Then the other option is to take the remote server address as a Command line parameter and assign to

String remoteServer = args[0]; // Check for null before this.

How do you add in the command line? Do you not need to add a try\catch incase the remoteserver address stated by the user is not found
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
Comment Utility
What I meant was to ask the user to provide the remote server address while running your application some thing like this.

C:\> java DVDClient  172.16.0.131

this value is accessible inside the program
if(args.length>0)
{
if(args[0] != null)
String remoteServer = args[0];
}
else
//some exception and exit.
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
change your dvd client code(String remoteServer = "localhost"; //"d065.infc.ulst.ac.uk";) like


       
      String remoteServer = null;//"localhost"; //"d065.infc.ulst.ac.uk";
        if(args!=null && args.length==1)
        {
              remoteServer=args[0];
        }
        else
        {
              System.out.println("Please provide the server ip address");
              System.out.println("Usage : java DVDClient <server ip address>");
              System.out.println("example : java DVDClient 192.168.1.1");
              System.exit(0);
        }
0
 

Author Comment

by:bigbong00
Comment Utility
Ive done this and the client simply prints out those lines above(below) and stops. The user isnt prompted for the ip address and following this should be presnted with the options a, b, c etc

import java.io.*;
import java.net.*;

public class DVDClient {

    public static void main(String[] args) throws IOException {

        Socket serverSocket = null;
        PrintWriter toServer = null;
        BufferedReader fromServer = null;
        String remoteServer = null;     //"localhost "d065.infc.ulst.ac.uk";
        if(args!=null && args.length==1)
        {
              remoteServer=args[0];
        }
        else
        {
              System.out.println("Please provide the server ip address");
              System.out.println("Usage : java DVDClient <server ip address>");
              System.out.println("example : java DVDClient local host");
              System.exit(0);
        }
       
        try {..........

Any idea why the code is not picking up the accounts I havve created within the class Admin
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
you have a main method in admin which creates the accounts and you are not calling it..

instead have the constructor of the admin create the accounts
0
 

Author Comment

by:bigbong00
Comment Utility
I have here in Admin.java

public class Admin
{

      // DECLARING THE USER ACCOUNT VARIABLES
      private static final int MAX_ACCOUNTS = 100;
      private int currentTotal = 0;
      private int currentAccNum = 1;

      Renter renters[] = new Renter[MAX_ACCOUNTS];

      // declaring our DVD VARIABLES
      private final int MAX_DVD = 1000;
      private int currentDVDTotal = 0;
      private int currentUniqueID = 1;

      DVD DVDs[] = new DVD[MAX_DVD];

      public synchronized int newAccount(String forename, String surname,
              String username, int initialBalance)
      {
            // Generates a new account and returns the account
            // number if successful, otherwise returns 0.
            if (currentTotal < MAX_ACCOUNTS)
            {
                  currentAccNum++;
                  renters[currentTotal++] = new Renter(forename, surname, username,
                          initialBalance, currentAccNum);
                  return currentAccNum;
            }
            else
            {
                  return 0;
            }
      } // newAccount

      public synchronized int newDVD(String title, String classification,
              String leadingCharacter, String director, boolean available,
              int price)
      {
            // Generates a new account and returns the account
            // number if successful, otherwise returns 0.
            if (currentDVDTotal < MAX_DVD)
            {
                  currentUniqueID++;

                  DVDs[currentDVDTotal++] = new DVD(title, classification,
                          leadingCharacter, director, available, price,
                          currentUniqueID);
                  return currentUniqueID;
            }
            else
            {
                  return 0;
            }
      } // newAccount

      public synchronized int getBalance(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getBalance();
            }
            else
            {
                  return 0;
            }
      } // getBalance

      public synchronized String getForename(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getForename();
            }
            else
            {
                  return "";
            }
      } // getforename

      public synchronized String getSurname(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getSurname();
            }
            else
            {
                  return "";
            }
      } // getSurname

      public synchronized String getUsername(int accNum)
      {
            Renter acc;
            acc = findAcc(accNum);
            if (acc != null)
            {
                  return acc.getUsername();
            }
            else
            {
                  return "";
            }
      } // getSurname

      public synchronized int credit(int Renter, int amount)
      {
            Renter acc;
            acc = findAcc(Renter);
            if (acc != null)
            {
                  acc.credit(amount);
                  return getBalance(Renter);
            }
            else
            {
                  return 0;
            }
      } // deposit

      public synchronized int debit(int Renter, int amount)
      {
            Renter acc;
            acc = findAcc(Renter);
            if (acc != null)
            {
                  acc.debit(amount);
                  return getBalance(Renter);
            }
            else
            {
                  return 0;
            }
      } // debit

      public synchronized String convertToString()
      {
            String str = "";

            for (int i = 0; i < currentTotal; i++)
            {
                  str = str + renters[i].convertToString() + "\n";
            }
            return str;
      } // convertToString

      private synchronized Renter findAcc(int accountNum)
      {
            for (int i = 0; i < currentTotal; i++)
            {
                  if (renters[i].getAccountNum() == accountNum)
                        return renters[i];
            }
            return null;
      }

      // Initialisation block - create a few accounts
      public static void main(String[] args)
      {
            Admin myadmin = new Admin();
            myadmin.newAccount("Ali", "Shaw", "ashaw", 100);
            myadmin.newAccount("Aaron", "Boyle", "aboyle", 200);
            myadmin.newAccount("Cilla", "Black", "cblack", 10);
            myadmin.newAccount("Hugh", "Brown", "hBrown", 50);
            myadmin.newAccount("Barry", "White", "BWhite", 80);
      } // Initialisation block

} // admin

class Renter
{

      private String forename;
      private String surname;
      private String username;
      private int balance;
      private int accountNum;

      Renter(String forename, String surname, String username,
              int initialBalance, int accountNum)
      {

            forename = forename;
            surname = surname;
            username = username;
            balance = initialBalance;
            this.accountNum = accountNum;
      } // Renter

      public int getBalance()
      {
            return balance;
      }

      public int getAccountNum()
      {
            return accountNum;
      }

      public void credit(int amount)
      {
            balance += amount;
      }

      public void debit(int amount)
      {
            balance -= amount;
      }

      public String getForename()
      {
            return forename;
      }

      public String getSurname()
      {
            return surname;
      }

      public String getUsername()
      {
            return forename;
      }

      public String convertToString()
      {
            return (forename + "\t" + surname + "\t" + username + "\t" + accountNum
                    + "\t" + balance);
      }

} // Renter

class DVD
{

      private String title;
      private String classification;
      private String leadingCharacter;
      private String director;
      private boolean available;
      private int price;
      private int uniqueID;

      DVD(String title, String classification, String leadingCharacter,
              String director, boolean available, int price, int uniqueID)
      {

            this.title = title;
            this.classification = classification;
            this.leadingCharacter = leadingCharacter;
            this.director = director;
            this.price = price;
            this.available = available;
            this.uniqueID = uniqueID;
      } // DVD

      public int getUniqueID()
      {
            return uniqueID;
      }

      public void credit(int amount)
      {
            price += amount;
      }

      public void debit(int amount)
      {
            price -= amount;
      }

      public String getTitle()
      {
            return (title);
      }

} // admin

I f you think this is wrong then where, what file and how will I change it?
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
replace the following block

public static void main(String[] args)
      {
            Admin myadmin = new Admin();
            myadmin.newAccount("Ali", "Shaw", "ashaw", 100);
            myadmin.newAccount("Aaron", "Boyle", "aboyle", 200);
            myadmin.newAccount("Cilla", "Black", "cblack", 10);
            myadmin.newAccount("Hugh", "Brown", "hBrown", 50);
            myadmin.newAccount("Barry", "White", "BWhite", 80);
      } // Initialisation block

as
Admin()
      {
            this.newAccount("Ali", "Shaw", "ashaw", 100);
            this.newAccount("Aaron", "Boyle", "aboyle", 200);
            this.newAccount("Cilla", "Black", "cblack", 10);
            this.newAccount("Hugh", "Brown", "hBrown", 50);
            this.newAccount("Barry", "White", "BWhite", 80);
      } // Initialisation block
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
also change renter constructor

 Renter(String forename, String surname, String username,
              int initialBalance, int accountNum)
      {

            this.forename = forename;
            this.surname = surname;
            this.username = username;
            this.balance = initialBalance;
            this.accountNum = accountNum;
      } // Renter
0
 

Author Comment

by:bigbong00
Comment Utility

Cheers. It is working now. Many thanks. Back to the original question

The client starts up and is presented with these options below. Here is the code

......................
        do {
              System.out.println("Please select one of the following options");
              System.out.println("a. Add new renter account");
              System.out.println("b. Retrieve a renters details");
              System.out.println("c. Deduct oustanding balance of a renter");
              System.out.println("d. Add to a renters oustanding balance");            
              System.out.println("e. List all the DVD's");
              System.out.print("Option: ");
            userInput = stdIn.readLine();

.................................

Though even before the client gets here I want them to state the address of the server to which they wish to connect to. The solutions above either didn't work or I was placing them in the wrong file or position in the code.

        String remoteServer = null;     //"localhost "d065.infc.ulst.ac.uk";
        if(args!=null && args.length==1)
        {
              remoteServer=args[0];
        }
        else
        {
              System.out.println("Please provide the server ip address");
              System.out.println("Usage : java DVDClient <server ip address>");
              System.out.println("example : java DVDClient local host");
              System.exit(0);
        }

I added the above code and the client simply ran. There was no prompt for the user to enter an ip adress. Then it stopped
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
you need to star the program like java dvdclient 100.154.343.543
0
 
LVL 17

Accepted Solution

by:
contactkarthi earned 500 total points
Comment Utility
if you want to prompt the user then

bring the following line above the try block where you connect to the server

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

then add the following lines
System.out.println("Please provide the server ip address");
remoteServer=stdIn.readLine();

this should do it
0
 

Author Comment

by:bigbong00
Comment Utility
I'm impressed. It works. Many Thanks. Though my questioning will continue though for now this is the solution
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now