1

I have been trying to construct a while loop for looping through a string when it contains a 'pattern' i'm looking for. The string is a local variable, declared just above the while loop and I am unable to substring it within my while loop, so that each consecutive loop will look at the next part of the string.

I would appreciate any help on how I could solve this problem

Here's the code; just so u have the idea the onlineList usually comes as array list output e.g. [Adrian, Bob, Buddy]

                String onlineList = networkInput.nextLine();
                //Declare a local variable for modified online list, that will replace all the strings that contain ", " "[" and "]"
                String modifiedOnlineList = onlineList.replaceAll("\\, ", "\n").replaceAll("\\[", "").replaceAll("\\]", "");
                //Loop the modifiedOnlineList string until it contains "\n"
                while (modifiedOnlineList.contains("\n")) {
                    //A local temporary variable for the first occurence of "\n" in the modifiedOnlineList
                    int tempFirstOccurence = modifiedOnlineList.indexOf("\n");
                    //Obtain the name of the currently looped user
                    String tempOnlineUserName = modifiedOnlineList.substring(0, tempFirstOccurence);
                    //Substring the remaining part of the string.
                    modifiedOnlineList.substring(tempFirstOccurence + 2);
                    System.out.println(modifiedOnlineList);

                }
3
  • You told us what you want your code to do, but didn't tell us what it's actually doing. Commented Oct 25, 2013 at 15:04
  • What I want to achieve at the end is adding the tempOnlineUserName into a new JList's component name, this would probably work already however i'm stuck in infinite loop Commented Oct 25, 2013 at 15:16
  • Infinite loop somewhere in the question helps. Anyway, all 3 answers provided will address, and fix your infinite loop Commented Oct 25, 2013 at 15:17

3 Answers 3

1

String is immutable in java

 modifiedOnlineList = modifiedOnlineList.substring(tempFirstOccurence + 2);

You have to receive the new String Object returned by substring method.

 modifiedOnlineList.substring(tempFirstOccurence + 2);
 System.out.println(modifiedOnlineList);   // still old value 

when you receive that

 modifiedOnlineList = modifiedOnlineList.substring(tempFirstOccurence + 2);
 System.out.println(modifiedOnlineList);   // now re assigned to substring value 
Sign up to request clarification or add additional context in comments.

Comments

1

Strings are immutable. That means that substring does not modify the string itself, but returns a new string object. So you should use:

modifiedOnlineList = modifiedOnlineList.substring(tempFirstOccurence + 2);

Comments

0

modifiedOnlineList.substring() just returns a substring of the original modifiedOnlineList, it doesn't modify modifiedOnlineList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.