Results 1 to 11 of 11
  1. #1
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default Check one array entry to each other entry in another array?

    If possible please could i get a bit of guidance.
    There are two (at least) problems with this code i've written.
    The arguments get broken down and stored into array, no problems.
    The problem is that in the if loop how can i get the array[n] to check in systematically against each entry in the arrayTest[g] argument?????

    Edit, i have tried recoding this method three different ways, using a forEach loop, while loop and nested for loops. And i've tried splitting each string down to individual letters and comparing them one at a time as suggested in the English to text translator thread on this forum, but each time it comes back down to the If loop not functioning as it should, but it's got me well stumped.

    Thanks in advance for any help given.


    Java Code:
       public static String testMethod(String input, String testInput)
       
       {
          int n = 0;
          int m = 0;
          int f = n+1;
          String[] array = new String[input.length()];
          String[] testArray = new String[testInput.length()];
          StringBuilder result = new StringBuilder();
            
             for(int p =0; p < array.length; p++) // take first argument and store in array
             {
                array[p] = input.substring(p,p+1);
             } 
             
             for(int q = 0; q < testArray.length; q++) // take second argument and store in array
             {
                testArray[q] = testInput.substring(q,q+1);
             }
             
            for (int g = 0; g < testArray.length; g++) // compare first array entry with second array
                                                       // entry if they match. Add entry to string builder
                                                       // result
             
                                                       
              {
                if (array[n].equals(testArray[g]))  // How to make this loop compare n to each value of
                                                    // g until it finds a match or reaches the end of
                                                    // the testArray????
                {
                 
                 System.out.println("for loop");
                 result.append(testArray[g]);
                 n++;
                 
                }
                   else  // If first and second arrays don't match check next
                         // array element to see if that matches.
                   {
                   g -= n;
                   n = 0; // As soon as this becomes n++; i get out of index issues
                  result.append(array[g]);
                }
                   
                }
             
                    String returnResult = new String(result); 
                   return returnResult;
          
          }
    Last edited by javadingbat; 02-11-2011 at 05:00 PM.

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    I have some practical questions:

    1) Why not just have the method implementation be:

    Java Code:
    if (input.equals(testInput)) {
      return input;
    } else {
      return "";
    }
    ?

    2) Since you're writing a test, you should consider using JUnit 4.x, in which case it would look like:

    Java Code:
    @Test
    public void testMethod() {
      String testString = <get your test string somehow>;
      String expectedString = <get your expected string somehow>;
      Assert.assertEquals(testString, expectedString);
    }
    If you MUST do an arraywise comparison, then you'd do it like this:

    Java Code:
    byte[] array = input.getBytes();
    byte[] testArray = testString.getBytes();
    
    if (array.length == testArray.length) {
      for (int i = 0; i < array.length; ++i) {
        if (array[i] != testArray[i]) {
          return "";
        }
      }
    }
    // in all other cases,
    return input;

  3. #3
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    [/CODE]

    If you MUST do an arraywise comparison, then you'd do it like this:

    Java Code:
    byte[] array = input.getBytes();
    byte[] testArray = testString.getBytes();
    
    if (array.length == testArray.length) {
      for (int i = 0; i < array.length; ++i) {
        if (array[i] != testArray[i]) {
          return "";
        }
      }
    }
    // in all other cases,
    return input;[/QUOTE]


    Cheers, but what happens if the array lengths are different?
    And does it make a difference you have done ++i rather than i++???

    I've played around with but i'm still getting loads of index out of bounds exceptions.

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by javadingbat View Post
    Cheers, but what happens if the array lengths are different?
    Then the test fails.


    Quote Originally Posted by javadingbat View Post
    And does it make a difference you have done ++i rather than i++???
    Nope, not here.

    Quote Originally Posted by javadingbat View Post
    I've played around with but i'm still getting loads of index out of bounds exceptions.
    Check your for loop.

  5. #5
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    Then the test fails.

    How can i make run if the arrays are different length?

  6. #6
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by javadingbat View Post
    How can i make run if the arrays are different length?
    It's a test; shouldn't the test fail if they're different lengths?

  7. #7
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    It's a test; shouldn't the test fail if they're different lengths?

    No, the whole method is supposed to break down the input string argument into an array, called testArray, for example.

    Then it should compare each entry in testArray against all entries in newArray, if testArray should have an corresponding entry in the multidimensional newArray it should return newArrays other entry for that particular instance.
    If testArray searches through newArray and does not find a suitable replacement the method should return the testArray value.

    Thus the arrays could be different lengths and I still have the problem that I can't get the entry in testArray to check against each value in newArray make the change if it should or return testArrays value if it doesn't find a replacement newArray and then start all over again with the next value in testArray.

  8. #8
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by javadingbat View Post
    How can i make run if the arrays are different length?
    Quote Originally Posted by javadingbat View Post
    No, the whole method is supposed to break down the input string argument into an array, called testArray, for example.

    Then it should compare each entry in testArray against all entries in newArray, if testArray should have an corresponding entry in the multidimensional newArray it should return newArrays other entry for that particular instance.
    If testArray searches through newArray and does not find a suitable replacement the method should return the testArray value.

    Thus the arrays could be different lengths and I still have the problem that I can't get the entry in testArray to check against each value in newArray make the change if it should or return testArrays value if it doesn't find a replacement newArray and then start all over again with the next value in testArray.
    Oh, oh, now I think get it. That was not at all clear before. Again, why are you using arrays? Why not just do:

    Java Code:
    // this is where your newArray values go
    Map<String, String> replacements = new TreeMap<String, String>();
    
    if (replacements.hasKey(testStr)) {
      return replacements.get(testStr);
    } else {
      return testStr;
    }
    Is that the gist of it?

    In any event, doing your own pairwise character comparison for Strings is utterly pointless, as that's exactly what the String's own implementation of equals() does.

  9. #9
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post

    Java Code:
    // this is where your newArray values go
    Map<String, String> replacements = new TreeMap<String, String>();
    
    if (replacements.hasKey(testStr)) {
      return replacements.get(testStr);
    } else {
      return testStr;
    }
    Is that the gist of it?
    It might be, sorry to be a newbie about this but i'm not at the map, treemap stage so i don't totally understand the code.

    What i'm really looking for is a simple example of code that shows how to structure a loop so that one array value will test against all other values in a second array and return either the original value or the corresponding value in the second array.
    Got something like that up your sleeve please?????

  10. #10
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by javadingbat View Post
    It might be, sorry to be a newbie about this but i'm not at the map, treemap stage so i don't totally understand the code.

    What i'm really looking for is a simple example of code that shows how to structure a loop so that one array value will test against all other values in a second array and return either the original value or the corresponding value in the second array.
    Got something like that up your sleeve please?????
    I give up; sorry.

  11. #11
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. I am not able to make entry into database
    By biradarashish in forum JDBC
    Replies: 18
    Last Post: 03-29-2010, 11:57 AM
  2. Save data entry
    By tiger100plus in forum NetBeans
    Replies: 2
    Last Post: 12-08-2009, 03:53 PM
  3. Entry in table
    By Harsh_Verma in forum New To Java
    Replies: 2
    Last Post: 07-01-2009, 03:07 PM
  4. Need Help On Entry Validation
    By obdi in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 07:39 AM
  5. need help checking monthlyRate entry
    By lowpro in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •