title: java: 'string index out of range: -1' exception using indexOf()
body: <p>Weird problem. I run this (very elementary) procedure to find a username and password in a file, and the program should compare the password entered to the password saved. Every time, however, i get a strange <code>String index out of range: -1</code> exception. I've suffered a similar problem before, however this time the <code>indexOf('.')</code> call is returning <code>-1</code>; which it doesn't like. Why is <code>indexOf()</code> returning <code>-1</code> if it causes an error? Here's the source:</p>
  
  <pre><code>public String loginToClient() throws FileNotFoundException, IOException {
          //decryptUsers();
          int tries;
          tries = 5;
          while (tries &gt; 0) {
              System.out.println("LOGIN");
              String usnm = c.readLine("Username: ");
              char [] passwd = c.readPassword("Password: ");
              users = new FileInputStream("users.fra");
              DataInputStream dis = new DataInputStream(users);
              BufferedReader br = new BufferedReader(new InputStreamReader(dis));
              String logindat = br.readLine();
              System.out.println(logindat);
              if (logindat.contains(usnm) == null) {
                  System.err.println("Username not recognised, please try another or create user.");
                  usnm = "INV";
                  return usnm;
              }
              else {
                  int startUsnm = logindat.indexOf(usnm);
                  System.out.println("startUsnm: " + startUsnm);
                  String logdat = logindat.substring(startUsnm, logindat.indexOf("."));
                  System.out.println("logdat: " + logdat);
                  int endUsnm = logdat.indexOf(':'); 
                  System.out.println("endUsnm: " + endUsnm);
                  int usnmend = endUsnm - 1;
                  System.out.println("usnmend: " + usnmend);
                  int startPass = endUsnm + 1;
                  System.out.println("startPass: " + startPass);
                  int endPass = logdat.indexOf('.');
                  System.out.println("endPass: " + endPass);
                  String Usnm = logdat.substring(0, usnmend);
                  System.out.println("Usnm: " + Usnm);
                  int passend = endPass - 1;
                  System.out.println("passend: " + passend);
                  String Pass = logdat.substring(startPass, passend);
                  System.out.println("Pass: " + Pass);
                  char [] Passwd = Pass.toCharArray();
                  if (usnm.equals(Usnm)) {
                      if (Arrays.equals(passwd,Passwd)) {
                          System.out.println ("Logged in. Welcome, " + usnm + ".");
                          String data = "LOGIN: " + usnm;
                          printLog(data);
                          //encryptUsers();
                          return usnm;
                      }
                      else {
                          System.out.println ("Incorrect password, please try again.");
                          String data = "PASWFAIL: " + usnm;
                          printLog(data);
                          tries -= 1;
                      }
                  }
                  else {
                      System.out.println ("Username not recognised.");
                      printLog("USNAMFAIL");
                      usnm = "INV";
                      return usnm;
                      //encrytUsers();
                  }
              }
          }
          //encryptUsers();
          System.exit(2);
          return usnm;
      }
  </code></pre>
  
  <p>And here's some input/output:</p>
  
  <pre><code>Startup initiated.
  Logfile exists.
  Users file exists.
  New user? n
  ELSE
  LOGIN
  Username: rik
  Password: 
  rik:55.
  startUsnm: 0
  endUsnm: 3
  startPass: 4
  endPass: -1
  Usnm: rik
  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -5
      at java.lang.String.substring(String.java:1949)
      at client0_0_2.loginToClient(client0_0_2.java:103)
      at client0_0_2.general(client0_0_2.java:209)
      at client0_0_2.&lt;init&gt;(client0_0_2.java:221)
      at client0_0_2.main(client0_0_2.java:228)
  </code></pre>
  
  <p>EDIT : SOLUTION FOUND!</p>
  
  <p>For some reason, indexOf() does not want to find a '.'- when replaced with a hyphen('-'), however, it runs perfectly, seemingly!</p>