-2

In a client server programm i have this line

    String line = inputStream.readLine();
    if (line.startsWith("/adduser")) {
      //do code
    }

example command " /adduser -id 1022 -p SomePass " I know how to begin the process by the line.startswith , but how can i make the program to read what is after -id and put it in a value and same with -p.

Syarx
  • 69
  • 1
  • 8

2 Answers2

0

You could split the string and use a for loop to parse the resulting array for flags.

String line = inputStream.readLine();
   if (line.startsWith("/adduser")) {
      String[] arguments = line.split(" ");
      int id = 0;
      String password = "";

      for (int i = 0; i < arguments.length - 1; i++)
         if (arguments[i].equals("-id"))
            id = Integer.parseInt(arguments[i+1]);    // Assuming that your id values are integers.
         else if (arguments[i].equals("-p"))
            password = arguments[i+1];

      // Handle values
 }
Sam Dindyal
  • 16
  • 1
  • 4
0

This should do the work. I would suggest using some tests on those id and pass values before doing anything fancy with them.

    String[] line = "/adduser -id 1022 -p SomePass".split(" ");
    String id = line[2];
    String somePass = line[4];
Daniel
  • 980
  • 9
  • 20