i have programming assignment which is to make UDP and TCP client
now i already finished UDP ,TCP client and GUI now i want to make the command line class
So the assignment tell me that

"CLIENT: The client should read input from the command line: 
•   -x <number>, where number is a 32-bit unsigned integer 
•   -t udp or tcp 
o   for tcp,  the client opens a TCP connection to the server 
o   for udp, the client sends the data to the server using UDP 
•   -s host name of the server (ex: dmx.cs.colostate.edu) 
•   -p the port being used by the server 
If any of the above command line arguments are missing, the program must print an error message and exit. The command arguments may appear in any order"

so what do i need to do to assign any value that appear in any order?
ArrayList<>? or any suggestion please help

Edited 3 Years Ago by mike_2000_17: Fixed formatting

what do you mean?
you have your command line args, they are stored in your
args array

public static void main(String[] args){

}

i want like when i put the value in any order -x123-ttcp-s-p10001
or -ttcp-s-p or whatever and then the program will execute into the port and all the method that i have What you will use to get these value into the right declaration?
sorry if my english really bad

well ... let's say you put in your command prompt:
java runProg -x123 cct -p100
in your main method, this code

System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);

will produce:
-x123
cct
-p100

i want like when i put the value in any order -x123-ttcp-s-p10001
or -ttcp-s-p or whatever and then the program will execute into the port and all the method that i have What you will use to get these value into the right declaration?
sorry if my english really bad

well to put them into the right declartion you'd do something like"

String number=args[0];//x is the first argument and relates to the 32bit unsigned number and after declartion would hold x123

Edited 4 Years Ago by DavidKroukamp: n/a

and how about i put -p instead of number=args[0]the program will execute[0] for -P? or -X

do you understand how to pass command line arguments to the application?
you've been given two examples above.

or, you could check this

and how about i put -p instead of number=args[0]the program will execute[0] for -P? or -X

well what argument number is p? if it was as stultuske said:
java runProg -x123 cct -p100

we know that there are now 3 arguments, and to get to -p100 we ask ourself what argument number is it? argument no.3, therefore in java we start at 0, so 3-1 =2, so therefore to parse -p100 to declaration we use:

String pargument=args[2];//this will now hold -p100

but check the link stultuske gave you, nothing can beat java docs :)

Edited 4 Years Ago by DavidKroukamp: n/a

thank you that's helped me alot :)
can you guide me a little more that how to change my code to command line
here is my code

package test;

import java.io.IOException;
import java.util.*;
import java.net.*;

public class MainCMD {
    
    
    public static void main (String args[]) throws SocketException, UnknownHostException, IOException{
        
      
         String cmd = null;
       
        DatagramSocket sock = new DatagramSocket ();
        Scanner keyboard = new Scanner (System.in);
       
        System.out.println("Enter //h for help");
        
        System.out.println("-t");
        cmd=keyboard.nextLine();
        
       
        
        if(cmd.equals("//h")){
            System.out.println("-t : UDP or TCP");
            System.out.println("-x : the integer that will send");
            System.out.println("-s : host name of the server ");
            System.out.println("-p : the port being used by the server ");
        }
        
        if(cmd.equals("udp")){
        
         
        try{
            int servPort = 0;
            String servName = null;
            System.out.println("Enter port");
             servPort=keyboard.nextInt();
          if(servPort > 10000 && servPort < 65536){
        
    
   
        UDPCliet udp = new UDPCliet(sock,servName,servPort);
        System.out.println("-x :");
        
        udp.makeRequest();
        udp.sendRequest();
	udp.getResponse();
	udp.useResponse();
	udp.close();
       }
          else{
              System.out.println("Port Number Must Be Between 10001 to 65535");
              
          }
        }catch(NumberFormatException e){
              
          System.out.println("Port must be only Integer");
        }    
     
        }
      
    
        if(cmd.equals("tcp")){
            
        try{
            int servPort = 0;
            String servName = null;
            System.out.println("Enter port");
            
            servPort=keyboard.nextInt();
          
            if(servPort > 10000 && servPort < 65536){
        
    
   
        TCPClient tcp = new TCPClient(servName,servPort);
        System.out.println("-x :");
        
              tcp.makeRequest();
              tcp.sendRequest();
              tcp.getResponse();
              tcp.useResponse();
              tcp.close();
              
       }
          else{
              System.out.println("Port Number Must Be Between 10001 to 65535");
              
          }
        }catch(NumberFormatException e){
              
          System.out.println("Port must be only Integer");
        }    
            
            
            
            
            
        }

        
        
        }

}

thank you that's helped me alot :)
can you guide me a little more that how to change my code to command line
here is my code

package test;

import java.io.IOException;
import java.util.*;
import java.net.*;

public class MainCMD {
    
    
    public static void main (String args[]) throws SocketException, UnknownHostException, IOException{
        
      
         String cmd = null;
       
        DatagramSocket sock = new DatagramSocket ();
        Scanner keyboard = new Scanner (System.in);
       
        System.out.println("Enter //h for help");
        
        System.out.println("-t");
        cmd=keyboard.nextLine();
        
       
        
        if(cmd.equals("//h")){
            System.out.println("-t : UDP or TCP");
            System.out.println("-x : the integer that will send");
            System.out.println("-s : host name of the server ");
            System.out.println("-p : the port being used by the server ");
        }
        
        if(cmd.equals("udp")){
        
         
        try{
            int servPort = 0;
            String servName = null;
            System.out.println("Enter port");
             servPort=keyboard.nextInt();
          if(servPort > 10000 && servPort < 65536){
        
    
   
        UDPCliet udp = new UDPCliet(sock,servName,servPort);
        System.out.println("-x :");
        
        udp.makeRequest();
        udp.sendRequest();
	udp.getResponse();
	udp.useResponse();
	udp.close();
       }
          else{
              System.out.println("Port Number Must Be Between 10001 to 65535");
              
          }
        }catch(NumberFormatException e){
              
          System.out.println("Port must be only Integer");
        }    
     
        }
      
    
        if(cmd.equals("tcp")){
            
        try{
            int servPort = 0;
            String servName = null;
            System.out.println("Enter port");
            
            servPort=keyboard.nextInt();
          
            if(servPort > 10000 && servPort < 65536){
        
    
   
        TCPClient tcp = new TCPClient(servName,servPort);
        System.out.println("-x :");
        
              tcp.makeRequest();
              tcp.sendRequest();
              tcp.getResponse();
              tcp.useResponse();
              tcp.close();
              
       }
          else{
              System.out.println("Port Number Must Be Between 10001 to 65535");
              
          }
        }catch(NumberFormatException e){
              
          System.out.println("Port must be only Integer");
        }    
            
            
            
            
            
        }

        
        
        }

}

well the first thing is when your arguments get parsed to the program you should be able to read them thats the most important:

if (args.length > 0) {//this is important if no arguments where given a small help or information should pop up
            for (int i = 0; i < args.length; i++) {//iterate through all arguments
                System.out.println("Argument: " + i + " " + args[i]);
            }
        } else {//help should pop up here this menas no arguments were given
            System.out.println("No arguments given!");
            //help commands and syntax would be shown here
            System.exit(0);
        }

experiment a bit with the above code, it should help you to understand whats going on...

Edited 4 Years Ago by DavidKroukamp: n/a

but check the link stultuske gave you, nothing can beat java docs :)

except scissors ;)

@OP: there is not much special to do to 'catch' command line arguments, they're automatically stored in your args-array, which you find in the signature of your main method.

all you need to do is handle them the right way. as cOrRuPtG3n3t!x mentioned, it would indeed be handy to know how many there are, before you try to handle them. everything you need to know to handle and deal with them is already posted in this thread. can you get any further at this point?

Comments

@stultuske haha very true :).

@OP: here's a small example i made quick to help:

public static void main(String[] args) {
        String fisrtarg=null;
        if (args.length > 0) {//this is important if no arguments where given a small help or information should pop up
            for (int i = 0; i < args.length; i++) {
                System.out.println("Argument: " + i + " " + args[i]);
                if (i == 0) {
                    fisrtarg = args[i];
                }
            }
        } else {//help should pop up here this menas no arguments were given
            System.out.println("No arguments given!");
            System.out.println("use -e to exit");
            //help commands and syntax would be shown here
            System.exit(0);
        }
        if (fisrtarg.equals("-e")) {
            System.out.println("Requested to exit by using -e");
            System.exit(0);
        } else {
            System.out.println("No such argument!");
            System.out.println("use -e to exit program with no output at all except the argument given");
            System.exit(0);
        }
    }

Edited 4 Years Ago by DavidKroukamp: n/a

This question has already been answered. Start a new discussion instead.