Find Index of String Occurrence - Java Method Help

  • Context: Java 
  • Thread starter Thread starter JaysFan31
  • Start date Start date
  • Tags Tags
    Method String
Click For Summary
SUMMARY

The discussion focuses on creating a Java method to find the index of the first occurrence of one string within another without using the built-in indexOf method. The user provided an initial implementation that incorrectly modifies the original string, leading to semantic errors. Key insights include the need to limit search indexes to length1 - length2 and the suggestion to create a separate string comparison function to facilitate accurate matching. The user is advised to avoid overwriting the original string during the search process.

PREREQUISITES
  • Understanding of Java string manipulation
  • Familiarity with loops and conditional statements in Java
  • Knowledge of substring extraction in Java
  • Basic concepts of algorithm efficiency and search limits
NEXT STEPS
  • Implement a custom string comparison function in Java
  • Research Java substring methods and their implications
  • Explore algorithm optimization techniques for string searching
  • Study the differences between mutable and immutable objects in Java
USEFUL FOR

Beginner Java developers, programming students, and anyone looking to enhance their understanding of string manipulation and custom search algorithms in Java.

JaysFan31
Hi. I just started Java and need some help with a method involving strings.

I need to return the index of the start of the first occurrence of one string (parameter two) in another string (parameter one).

I tried this:
Code:
public static int findInString (String text1, String text2)
{
int length1 = text1.length();
int length2 = text2.length();
int index;
char x = text2.charAt(0);
for (index = 0; index < length1; index++)
{
char y = text1.charAt(index);
if (x == y)
{
text 1 = text1.substring(index, index + length2);
if (text1 == text2)
{
return index;
}
}
if (index == length1)
{
return -1;
}
}

NOTE: I cannot use the indexOf method. It's for a programming class at a local university and he won't let us use it. So I need to find an alternative way to basically do what it does.

My code compiles; it's just wrong semantically. Any help would be appreciated.
 
Technology news on Phys.org
Search indexes should be limited to length1 - length2, no point in searching past the point where there aren't enough bytes remaining in text1. If length1 < length2, then no match is possible.

Since this is just a test program, might as well create a string compare function, and then call it to compare &text1[test index], with text2, length of test2, until you get a match or increment test index > (length1-length2).
 
I think the basic problem is you are trying to use text1 for two different things. When you assign text1 to a substring of itself, you delete the end of the original string. For example you won't find xyz in abxcdxyz, because you find the first x and then overwrite the long string with xcd.
 
Last edited:

Similar threads

Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
8
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
10
Views
2K