Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: printing out usernames

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Thanked 0 Times in 0 Posts

    Default printing out usernames

    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.
    class  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){
                                    txtUser.append(input.substring(i+2) + "\n");
                            txtMes.append(input + "\n");
                 catch(Exception e){
    txtMes and txtUser are JTextAreas

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 697 Times in 679 Posts

    Default Re: printing out usernames

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Add some print statements to determine the actual values of the index and the cause of the problem. Apparently, your code isn't providing the results you'd anticipated.

Similar Threads

  1. Printing i+1
    By felixb in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2013, 04:27 AM
  2. printing
    By Gasper in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 16th, 2013, 04:57 AM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. How would you check for duplicate usernames in a file?
    By suxen in forum Java Theory & Questions
    Replies: 1
    Last Post: March 28th, 2011, 11:35 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread