I am trying to print out a list of usernames for my chat program. It currently works except for the username portion. The Server sends a specific line that tells the client to print it as a username. That line is
name//name. For example, if the name of the client was Eric, then the server would send Eric//Eric to the client. The client will then receive the line through a buffered reader. However, my current code keeps getting an error at the i=input.substring... line. Its an IndexOutOfBoundsException: String index out of range: -1.
Here's the part of my code that needs help.
txtMes and txtUser are JTextAreasclass ChatThread extends Thread { ChatServer cServer; String[] sentence; int i; public void run() { String input = "Eric"; try { while(true) { input = br.readLine(); i = input.substring(0,input.indexOf("//")).length(); String s = input.substring(i,i+2); if(input.substring(i, i+2).equals("//")){ sentence = txtUser.getText().split("\n"); for(String word: sentence){ if(word.equals(input.substring(i+2))){} else{ txtUser.append(input.substring(i+2) + "\n"); } } } else{ txtMes.append(input + "\n"); } } } catch(Exception e){ e.printStackTrace(); }