package test;


import java.util.*;
import sun.security.krb5.internal.UDPClient;
import java.net.*;


public class CMD {
    
        public static void main(String[] args) throws UnknownHostException, SocketException {
    
       
       DatagramSocket sock = new DatagramSocket ();     
       Scanner keyboard = new Scanner(System.in);
            
       String fisrtarg=null;
       
      // System.out.println("Enter CMD");
       
      // fisrtarg = keyboard.nextLine();
       
       
    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 if (args.length != 4  ){
        
        System.out.println("must Input 4 args : EXIT!");
        System.exit(0);
    }
    }
    
    
    } 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");
            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 ");
    System.exit(0);
    }
     if(fisrtarg.equals("-x")){
         
         
     }
     if(fisrtarg.equals("-t")){
        
         
         
     }
     if(fisrtarg.equals("-p")){
        
       
        try{
            int servPort = 0;
            String servName = null;
            servPort=keyboard.nextInt();
          if(servPort > 10000 && servPort < 65536){
         
         UDPCliet udp = new UDPCliet(sock,servName,servPort);
    
     }
         else{
              System.out.println("Port Number Must Be Between 10001 to 65535");
              
          }
        }catch(NumberFormatException e){
              
          System.out.println("Port must be only Integer");
        }    
     if(fisrtarg.equals("s")){
         
         
     }
   
    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);
    }
    
        
        
        }
    
}
}

Agin sorry for so many question in these day
I wanna know i'm getting into the right take for the command line arg ?

Recommended Answers

All 20 Replies

that depends on what arguments you pass.
for instance, it won't work if you pass: -x100 since this will be the argument.

also, you could optimize your test to check if the right number of arguments were entered:

switch(args.length){
  case 4: // handle your arguments here, or set a flag to true
          break;
  default: // this will be run if you have any other number of arguments
           // you can, for instance, exit the program after asking the user to
           // restart it with the right amount of arguments
}
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]);
    
    switch(args.length){
        
        case '-x': ;// handle your arguments here, or set a flag to true
        case '-t':
        case '-p':    
        case '-s':   
        break;
        
        default:

like this sorry i've never done this before

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]);
    
    switch(args.length){
        
        case '-x': ;// handle your arguments here, or set a flag to true
        case '-t':
        case '-p':    
        case '-s':   
        break;
        
        default:

like this sorry i've never done this before

well you are on the right track, next make 4 if statements and 4 variables to hold each argument, next the use 'firstarg.cahrAt(1);' and this should give you the first letter of your argument, now you may use the case statement for each arg like so

case 'x':{}

no.
args.length will always return an integer, not a String. (which should take double quotes).

if you are using java 7, you can use a switch block on a String as well, but that's another matter.

switch(args.length){
  case 4: handleArguments(args);
          break;
  default: System.out.println("Wrong number of arguments, try again");
           System.exit(0);
}

...

static void handleArguments(String[] arguments){
  for ( String arg : arguments){
    // if you have java 7
    switch(arg){
      case "-x": // handle this
                 break;
      case "-t": // handle this
                 break;
      ...
      default: System.out.println("invalid argument");

    }
  }
}

but, if you want your arguments always to be passed in a fixed order, you'll need to iterate over the array, and compare to the value of the argument based on what you expect as that argument

well you are on the right track, next make 4 if statements and 4 variables to hold each argument, next the use 'firstarg.cahrAt(1);' and this should give you the first letter of your argument, now you may use the case statement for each arg like so

case 'x':{}

the first char would always turn out to be '-'
my bad, charAt(1) will return the letter alright :)

the first char would always turn out to be '-' :)

why? it will only be '-' if i used firstarg.charAt(0); not firstarg.charAt(1); or am i missing something.

[EDIT]lol its cool stultuske

why? it will only be '-' if i used firstarg.charAt(0); not firstarg.charAt(1); or am i missing something

which is why I already corrected my post ;)

if(args.length != 4){
        System.out.println(" must enter 4 arguments");
        System.exit(0);
    switch(fisrtarg.charAt(1))
    
    {
       
        case '-x':{}// handle your arguments here, or set a flag to true
        case '-t':{}
        case '-p':{}    
        case '-s':{}   
        
        break;
        
        default:
    }

Ok?

really hard isn't it T_T

really hard isn't it T_T

please note though you cant use: '-x' its must be 'x' a char only contains a single letter not more or else then its a string :)

if(args.length != 4){
        System.out.println(" must enter 4 arguments");
        System.exit(0);
    switch(fisrtarg.charAt(1))
    
    {
       
        case '-x':{}// handle your arguments here, or set a flag to true
        case '-t':{}
        case '-p':{}    
        case '-s':{}   
        
        break;
        
        default:
    }

Ok?

no

you are comparing the value of one single char, but
'-x' and '-t' are each two chars.

you'll have to remove the - in your options
also, remove the brackets after your cases and end each case with break;, otherwise,
for instance, if you get x, you'll run all the code for t, p and s as well

i think i got it now
but for the argument -t what should i do for this case
-t is about choosing between sending the message by using UDP or TCP (because i have separate class)

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");
        }    
            
            
            
            
            
        }

        
        
        }

}

here is my main class can you lead me the way to put the method

I thought we were discussing command line arguments? did I mis something?

i think i got it now
but for the argument -t what should i do for this case
-t is about choosing between sending the message by using UDP or TCP (because i have separate class)

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");
        }    
            
            
            
            
            
        }

        
        
        }

}

here is my main class can you lead me the way to put the method

well make t always the last argument, and if t does not exists in the arguments instead of showing an error just dont? and what method are you talking about?

if(args.length != 4){
        System.out.println(" must enter 4 arguments");
        System.exit(0);
    switch(fisrtarg.charAt(1))
    
    {
       
        case 'x':// handle your arguments here, or set a flag to true
        case 't':if(fisrtarg.equals("udp")){
            String servName = null;
            int servPort = 0;
            UDPCliet udp = new UDPCliet(sock,servName,servPort);
                     }
           
        if(fisrtarg.equals("tcp")){
             int servPort = 0;
             String servName = null;
                  TCPClient tcp = new TCPClient(servName,servPort);  
                     } break;
        case 'p':    
        case 's':   
        
        break;
        
        default:
    }

yes ?

no
charAt(1) returns a single char, "tcp" and "udp" are String objects, arrays of chars

what exactly is it you put in as input for your arguments, and how do you want them dealt?

if(args.length != 4){
        System.out.println(" must enter 4 arguments");
        System.exit(0);
    switch(fisrtarg.charAt(1))
    
    {
       
        case 'x':// handle your arguments here, or set a flag to true
        case 't':if(fisrtarg.equals("udp")){
            String servName = null;
            int servPort = 0;
            UDPCliet udp = new UDPCliet(sock,servName,servPort);
                     }
           
        if(fisrtarg.equals("tcp")){
             int servPort = 0;
             String servName = null;
                  TCPClient tcp = new TCPClient(servName,servPort);  
                     } break;
        case 'p':    
        case 's':   
        
        break;
        
        default:
    }

yes ?

you are on the right track but look here at stultuske code(i editied it a bit) note this is a method:

static void handleArguments(String[] arguments){
  for ( String arg : arguments){
    // if you have java 7
    switch(arg.charAt(1)){
      case "x": // handle this
                 break;
      case "t": // handle this
      if(arg.contains("udp")){
      } else {//tcp
      }
                 break;
      ...
      default: System.out.println("invalid argument");
 
    }
  }
}

and yours:

switch(fisrtarg.charAt(1))
 
    {
 
        case 'x':// handle your arguments here, or set a flag to true
        case 't':if(fisrtarg.equals("udp")){
            String servName = null;
            int servPort = 0;
            UDPCliet udp = new UDPCliet(sock,servName,servPort);
                     }
 
        if(fisrtarg.equals("tcp")){
             int servPort = 0;
             String servName = null;
                  TCPClient tcp = new TCPClient(servName,servPort);  
                     } break;
        case 'p':    
        case 's':   
 
        break;
 
        default:
    }

look at the switch statement you are switching between the first argument only? you are supposed to go through each argument and then your on your way. to use the method just call it with the array of arguments

sorry i thought it were like this when i put -tudp in command line the program will execute that i'm using UDP to send the message so i put the condition statement

sorry i thought it were like this when i put -tudp in command line the program will execute that i'm using UDP to send the message so i put the condition statement

Im a bit confused now, putting the 'if' statement to check for tcp and udp is fine, i was showing you that your ' switch(fisrtarg.charAt(1))' is wrong it must be:

static void handleArguments(String[] arguments){//this is just the method to get the args sent to it via main
  for ( String arg : arguments){
    // beacause of the for and switch statement surrounding this comment it will now be able to process all arguments and not just the first like you were doing
    switch(arg.charAt(1)){
   }
 }
}

i'm downloading java 7 now and i think i'm gonna take my laptop to get my professor for advice me
thank you guy agin :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.