4

l'm trying to create two functions. One: that shows 'true' if the c variable appears at least ONCE in the String variable. Two: The same function but shows the position of the letter. More specifically,the seqSearchPos function (String s, char c) searches if c appears in s. function returns the position of c to s if it exists. If c does not exist the function will return -1.

l sadly do not know how to appoarch this problem, this specific problem regarding String and char.There must be a method that l still do not know that can help me in this matter

public static boolean seqSearch(String s, char c) {
       boolean found=false;
       if(s.equals(c)) {
           found=true;
       }
       return found;
   }

Main:

String s="e";
char c='e';
System.out.println(seqSearch(s,c));

public static int seqSearchPos(String s,char c) {
       int position=-1;
       for(int i=0; i<s.length(); i++) {
           if(s.equals(c)) {
               position=i;
               break;
           }
       }
       return position;
   }

Main:

String s="hello";
char c='e';
System.out.println(seqSearchPos(s,c));

I expected to show true in the first one and in the second the position 1 but it showed false and -1 respectively.

Patsakr
  • 41
  • 5
  • 1
    Hint: they are exactly the same method, and should work the same way. The only difference is what they return when they find/don't find the character. – RealSkeptic Apr 01 '19 at 14:01

4 Answers4

2

There is a bug in this line of code:

if(s.equals(c)) {

It is comparing the entire string with the character, and it (obviously) does not match.

You need to compare the single character at the index in the string:

if (s.charAt(i) == c) {

As an aside, you might also use return directly from the if statement. This will let you remove the position variable and make the code a bit shorter:

public static int seqSearchPos(String s, char c) {
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == c) {
            return i;
        }
    }
    return -1;
}

There is also an String.indexOf(int c) method that does exactly what you need and would let you write the method like this:

public static int seqSearchPos(String s, char c) {
    return s.indexOf(c);
}

Or, even better, simply call that String method directly instead of wrapping it in your own function.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • If it is possible,can you please explain the String.indexOf() in a more simple way? l can't seem to understand this nor the int indexOf(int ch). – Patsakr Apr 01 '19 at 14:33
  • 1
    In Java a `char` is actually an integer number (0 to 65535) and can be used like and assigned to an `int` (which has a much wider range). The `String.indexOf(int c)` method takes an `int` rather than a `char` so that it can handle modern Unicode characters like smilies that extend character codes beyond the bounds of a `char`. But you can still call that method with a `char`. – ᴇʟᴇvᴀтᴇ Apr 01 '19 at 15:24
1

You should be able to find your answers here: How can I check if a single character appears in a string?

One of the answers also talks about the position of the character.

What they're basically doing is using the default String library, namely: String.contains() and String.indexOf()

YVbakker
  • 130
  • 9
1

Actually you are trying to implement contains(char) and indexOf(char) methods of String class.

You are comparing whole string with single char.If string's lengths is not 1,your method will always return false.To correct this,you should iterate each char of String.And you can compare two char with == operator.

public static boolean seqSearch(String s, char c) {
   boolean found=false;
   for(int i=0;i<s.length();i++){
     if(s.charAt(i)==c) {
        found=true;
     }
   }
   return found;

}

Same problem exists in your second issue,you should compare each char.

public static int seqSearchPos(String s,char c) {
   int position=-1;
   for(int i=0; i<s.length(); i++) {
       if(s.charAt(i)==c) {
           position=i;
           break;
       }
   }
   return position;

}

Y.Kakdas
  • 833
  • 7
  • 17
0

As others pointed out your problem is that you compare the whole String s to the char c which is always false.

Here are two implementations of your methods using Java Streams:

public static boolean seqSearch(String string, char character) {
    return string.chars().anyMatch(c -> c == character);
}

This creates an IntStream containing all chars returning true if any of them is true.

public static int seqSearchPos(String string, char character) {
    return IntStream.range(0, string.length())
            .filter(i -> string.charAt(i) == character)
            .findFirst()
            .orElse(-1);
}

Here you create an IntStream from 0 to n - 1 and return the first index if the char is found. If not it returns -1.

Beside all of that you can just use the native String.indexOf() method, which does exactly what you want:

public static int seqSearchPos(String string, char character) {
    return string.indexOf(character);
}

You can also use this method for your seqSearch() method:

public static boolean seqSearch(String string, char character) {
    return string.indexOf(character) >= 0;
}

Alternatively you can use the native String.contains() method to achieve that:

public static boolean seqSearch(String string, char character) {
    return string.contains(String.valueOf(character));
}
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56