This site requires JavaScript, please enable it in your browser!
Greenfoot back
thekidj
thekidj wrote ...

2013/10/17

Contains one occurrence?

1
2
thekidj thekidj

2013/10/17

#
I know the contains method tests to see if a string contains at least one of the letters you entered, but how do I set it to test if there's one at the most?
bourne bourne

2013/10/17

#
You can't set the method to have an alternative behavior when it has no support for such. You will have to implement something. Looks like you could use the indexOf and lastIndexOf methods to get what you want. Something like:
String myString;
...
int firstIndex = myString.indexOf( something );
int lastIndex = myString.lastIndexOf( something );
if (firstIndex != -1 && firstIndex == lastIndex)
{
	// Only one occurrence of something in myString. Where something is another String or just a char
}
thekidj thekidj

2013/10/17

#
bourne wrote...
You can't set the method to have an alternative behavior when it has no support for such. You will have to implement something. Looks like you could use the indexOf and lastIndexOf methods to get what you want. Something like:
String myString;
...
int firstIndex = myString.indexOf( something );
int lastIndex = myString.lastIndexOf( something );
if (firstIndex != -1 && firstIndex == lastIndex)
{
	// Only one occurrence of something in myString. Where something is another String or just a char
}
Can you put that in terms of my code?
/**
     * Constructor for objects of class FriendList
     */
    public FriendList()
    {
        friend = new ArrayList<Person>();
    }

    public void add(Person p)
    {
        friend.add(p);
    }

    public Person get(int index)
    {
        if (index >= 0 && index < friend.size())
        {
            return friend.get(index);
        }
        else
        {
            return null;
        }
    }    

    public boolean purge(int index)
    {
        if (index >= 0 && index < friend.size())
        {
            friend.remove(index);
            return true;
        }
        else
        {
            return false;
        }
    }

    public void putLast(int index)
    {
        if (index >= 0 && index < friend.size())
        {
            Person end = friend.remove(index);
            friend.add(end);
        }
    }

    public void list()
    {
        int index = 0;
        while(index < friend.size())
        {
            Person x = friend.get(index);
            System.out.println("Friend's Name:  " + x.getName());
            System.out.println("Friend's Number:    D00" + x.getMemberNumber());
            System.out.println();
            index++;
        }
    }

    public void listContainingAtLeastOne(String str)
    {
        int index = 0;
        while(index <friend.size())
        {
            Person x = friend.get(index);
            if (x.getName().contains(str))
            {
                System.out.println(x.getName());
            }
            index++;
        }
    }

    public void listContainingAtMostOne(String str)
    {
       
    }
}
bourne bourne

2013/10/17

#
In your method listContainingAtMostOne(String str), do what you did in the method above it, but change the if statement to mine. While exchanging "something" with "str", and exchanging "myString" with "x.getName()".
bourne bourne

2013/10/17

#
If you want it to include those that do not contain (i.e. contains one or none) then change if (firstIndex != -1 && firstIndex == lastIndex) to if (firstIndex == -1 || firstIndex == lastIndex)
thekidj thekidj

2013/10/17

#
bourne wrote...
In your method listContainingAtMostOne(String str), do what you did in the method above it, but change the if statement to mine. While exchanging "something" with "str", and exchanging "myString" with "x.getName()".
What would I change the number to if I wanted exactly one character?
bourne bourne

2013/10/17

#
What number? And exactly one character of what? I believe you are under the impression that the contains method returns true if at least one char of the input is within the String. This is not the case. The contains method "Returns true if and only if this string contains the specified sequence of char values." "apple".contains("aple") // returns false "apple".contains("ee") // returns false "apple".contains("Apple") // returns false "apple".contains("apple") // returns true "apple".contains("pp") // returns true "apple".contains("e") // returns true
thekidj thekidj

2013/10/17

#
bourne wrote...
What number? And exactly one character of what? I believe you are under the impression that the contains method returns true if at least one char of the input is within the String. This is not the case. The contains method "Returns true if and only if this string contains the specified sequence of char values." "apple".contains("aple") // returns false "apple".contains("ee") // returns false "apple".contains("Apple") // returns false "apple".contains("apple") // returns true "apple".contains("pp") // returns true "apple".contains("e") // returns true
Just one string that I've input. Remove all names containing str exactly once.
bourne bourne

2013/10/17

#
My original if statement, if (firstIndex != -1 && firstIndex == lastIndex) gets its block of code executed if it is contained, and only once
thekidj thekidj

2013/10/17

#
bourne wrote...
My original if statement, if (firstIndex != -1 && firstIndex == lastIndex) gets its block of code executed if it is contained, and only once
For some reason it won't remove the names that it should remove
 int index = 0;
        while(index < friend.size())
        {
            Person x = friend.get(index);
            int firstIndex = x.getName().indexOf(str);  
            int lastIndex = x.getName().lastIndexOf(str);
            if (firstIndex != -1 && firstIndex == lastIndex)
            {
                friend.remove(x.getName());
            }
            index++;
        }
danpost danpost

2013/10/17

#
You probably wanted to use 'friend.remove(index)'. However, you should also, subtract one from the index immediately afterwards within the same 'if' block, so you do not skip over the next element in the list (which will now occupy the same index that you removed your last friend from).
bourne bourne

2013/10/17

#
Give me an example data (name of a Person) and an str that you would expect it to be removed
bourne bourne

2013/10/17

#
Thanks danpost, I didn't catch that. What danpost said.
danpost danpost

2013/10/17

#
No problem (that is: your welcome).
thekidj thekidj

2013/10/29

#
danpost wrote...
No problem (that is: your welcome).
My professor and I are going to look over the code later this week. Thanks for the help though!
There are more replies on the next page.
1
2