0

I'm trying to find if user input s contains the second user input s1 using for loops and if statements. I only want to use methods .charAt() and .length and not .indexOf to find if s1 is within s. Once I find whether s contains s1 or not, I either want to return .charAt() where s1 begins in s, or return -1 if s1 doesn't occur at all inside s.

For example, s = abcdefghi & s1 = def, it will return 3.

This is my code so far: It either returns 0 or -1

package javaapplication5;

import java.util.Scanner;
class myStrings {
String s, s1;

void setMyStrings(String str, String str1) {
    s = str;
            s1 = str1;
}

int find() {
    int i, j;
    int r = -1;
    char ns = s1.charAt(0);
    int sp;
    int count = 1;

    for (i = 0; i < s.length(); i++){
        if (s.charAt(i) == ns) {
            sp = i;//starting point
            for (j = i+1; j < s1.length(); j++) {
                ns += 1;
                if (s.charAt(j) == ns){
                    j++;
                    count++;
                }
                if (count == s1.length())
                    r = sp;
            }
        }   
    }
    System.out.println(r);
    return r;
   }
}
public class JavaApplication5 {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    myStrings setS = new myStrings();

    String s, s1;

    System.out.print("Enter s:");
    s = in.nextLine();
    System.out.print("Enter s1:");
    s1 = in.nextLine();

    setS.setMyStrings(s1, s1);
    setS.find();

}

}

0

3 Answers 3

1

:D, it is because this line in your main method

setS.setMyStrings(s1, s1);

You add the same value.

Also If I look good, this line

for (j = i+1; j < s1.length(); j++)

Should be written as this :

for (j = i+1; j < i+1+s1.length(); j++)

Also this line is wrong

ns += 1;

ns is char, it means, this for example change char A to B. You need something like this :

ns = s1.charAt(j-i)
Sign up to request clarification or add additional context in comments.

Comments

0

So you're trying to implement String.indexOf(String) yourself.

A place to start (or a way to cheat, depending on what your goal is) would be to look at the actual JDK implementation in class String: it delegates to a package-private static method which does exactly what you're trying to do:

static int indexOf(char[] source, int sourceOffset, int sourceCount,
                   char[] target, int targetOffset, int targetCount,
                   int fromIndex) {
    // ...

Comments

0

Java isn't C, you cannot go to next char with

ns += 1;

You have to do something like:

if (s.charAt(j) == s1.charAt(j-sp)){

1 Comment

Even in C, this will be wrong since you didn't reinitialize ns when inner loop fail to find the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.