Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default tricky indexOf implementation -- Help!!

    Hi there,

    I am implementing the indexOf method of the string class to check whether a string value exists in another as in the code below:

    Java Code:
    String str = "9,10,11,12";
    String isIn = JOptionPane.showInputDialog("Please enter a number");
     
    int x = str.indexOf(isIn);
     
    if(x != -1)
    {
        System.out.println("Not in");
    }
    else
    {
        System.out.println("in");
    }

    The problem here is that if the user enters "1", 1 is in the string as the number 11. How would I be able to write the code such that it can differentiate between 1 and 11? I have thought about it and I just cannot figure out how it can be done. I am new to java and I would appreciate some assistance in this regard.

    Thanks :confused:

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    You could just check the character right after the 1 that you find and make sure it isn't a 1 as well.

    Java Code:
    int i = String.indexOf("1");
    if( i != -1 && !String.charAt(i).equals("1")){
    
    }
    You could also do an indexOf("1") and an indexOf("11") and compare them. If they are the same then you have 11 and not 1.

    The problem with these solutions is that they are very specific. I would probably write my own method using the first technique. I would pass it the character to look for and it would return the position only if the character was repeated.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    String str = "9,10,11,12";
    String isIn = " ";
    do {
        isIn = JOptionPane.showInputDialog("Please enter a number");
        if(isIn == null) break;
    
        int x = str.indexOf(isIn);
    
        boolean confirmed = false;
        if(x != -1) {
            boolean hasLeadingComma = x > 0 && str.charAt(x-1) == ',';
            boolean hasTrailingComma = x < str.lastIndexOf(",") &&
                str.charAt(x + isIn.length()) == ',';
            boolean isFirst = x == 0 && hasTrailingComma;
            boolean isLast = x > str.lastIndexOf(",") && hasLeadingComma;
            confirmed = isFirst || isLast ||
                (hasLeadingComma && hasTrailingComma);
        }
    
        if(confirmed) {
            System.out.println("Found " + isIn + " at index " + x);
        } else {
            System.out.println(isIn + " not found, x = " + x);
        }
    } while(isIn.length() > 0);

  4. #4
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default Re:

    Thanks alot for your assistance, I do appreciate all the help that has been offered..

    :)

Similar Threads

  1. Custom ArraySet implementation
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:41 PM
  2. VietPad 2.0 (.NET implementation)
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-05-2008, 04:32 PM
  3. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 09:19 PM
  4. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:57 AM
  5. Help regarding indexOf
    By gauravj in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 01:12 PM

Posting Permissions

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