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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: Finding a substring from an array within a string argument

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Finding a substring from an array within a string argument

    Hi All,

    I'm new to Java and doing a course where I need to check to see if a substring from the first column of a 2 dimensional array exists in a String argument. If it does I should replace the substring in the argument string with the string that exists in the second column of the 2 dimensional array, on the same row as the one that was searched.

    Here's my code so far...

      public static String theClassMethod(String theStringArgument)
      {
         int foundAt = 0;
         for (int i = 0; i < variableArray.length; i++)
         {
            foundAt = theStringArgument.indexOf(variableArray[i][0]);
            System.out.println(i);
            if (foundAt > 0)
            {
               return replaceEach(theStringArgument, variableArray[i][0], variableArray[i][1]);
            }
          }
          return theStringArgument.toString();

    The code seems to work fine when the substring found in variableArray is the 2nd character in theStringArgument, but when it is the 1st character it doesn't find it, even though it's definately in one of those variableArray positions.

    I put the System.out.println(i) into the code so I code watch the iterations, and it just seems to skip over the substring in variableArray as though it doesn't see it.

    I assume I'm making some fundamental error, probably in the for loop somewhere but if anyone spots anything I'd be most obliged if you code let me know.

    Thanks all.

    Si.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Finding a substring from an array within a string argument

    Why do you check to make sure foundAt is greater than 0? I suggest you check out the API for String.indexOf() to see what it returns when the substring is not found and what it returns when the substring is found at the first character.

    But why don't you just use a method like replaceAll() instead of all this rigmarole?

    PS- Rigmarole is your word of the day.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 08:58 AM
  2. HELP array cmdline argument
    By eummm in forum Collections and Generics
    Replies: 2
    Last Post: November 26th, 2010, 03:21 AM
  3. Replies: 1
    Last Post: February 4th, 2010, 03:23 PM
  4. First string is a substring of another
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 21st, 2009, 10:35 PM
  5. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM