473,499 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with string method

19 New Member
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 occurence of one string (parameter two) in another string (parameter one).

I tried this:
Expand|Select|Wrap|Line Numbers
  1. public static int findInString (String text1, String text2)
  2. {
  3. int length1 = text1.length();
  4. int length2 = text2.length();
  5. int index;
  6. char x = text2.charAt(0);
  7. for (index = 0; index < length1; index++)
  8. {
  9. char y = text1.charAt(index);
  10. if (x == y)
  11. {
  12. text 1 = text1.substring(index, index + length2);
  13. if (text1 == text2)
  14. {
  15. return index;
  16. }
  17. }
  18. if (index == length1)
  19. {
  20. return -1;
  21. }
  22. }
  23.  
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.
Jun 1 '07 #1
8 1540
RedSon
5,000 Recognized Expert Expert
Try using .equals instead of ==
Jun 1 '07 #2
PvtBillPilgrim
19 New Member
Can you just tell me if this makes any sense?

I think it's fairly intelligent in just thinking about it. But I'm not sure if it's doing exactly what I want it to do.
Jun 2 '07 #3
r035198x
13,262 MVP
Can you just tell me if this makes any sense?

I think it's fairly intelligent in just thinking about it. But I'm not sure if it's doing exactly what I want it to do.
Use the .equals method for camparing Strings as has been suggested then test your code and you'll see whether it's correct or not.
Jun 2 '07 #4
JosAH
11,448 Recognized Expert MVP
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.
If you're planning to use the substring() method why not let that method do all
the work? Suppose you have a String S of length n and a pattern P of length m.

If m > n you're sure you can't find the pattern in String S. otherwise, for all
positions i in [0, n-m] take the substring of length m starting at position i and
compare that substring with your pattern P.

In other words, there's no need to search for that first character in S and P to
compare that for equality.

kind regards,

Jos
Jun 2 '07 #5
PvtBillPilgrim
19 New Member
OK. I'm trying this with no luck (but it looks fine to me).

I have the easy cases in my program; I'm just not going to include them here:
Expand|Select|Wrap|Line Numbers
  1. String mutation;
  2. int index = 0;
  3. while (index < (lengthStr - lengthSubstring))
  4. {
  5. mutation = str.substring(index, index + lengthSubstring);
  6. if (mutation.equals(substring))
  7. {
  8. position = index;
  9. break;
  10. }
  11. index++;
  12. }
  13. position = -1;
  14. }
  15. return position;
  16. }
  17.  
I get -1 everytime except for when the strings are equal (since the cases not included here work fine).

What am I doing wrong? It compiles, just doesn't work.
Jun 2 '07 #6
JosAH
11,448 Recognized Expert MVP
OK. I get -1 everytime except for when the strings are equal (since the cases not included here work fine).

What am I doing wrong? It compiles, just doesn't work.
Even if you set your 'position' variable to the correct value in your while loop
you destroy it again just after the loop again, i.e. you explicitly set it to -1.

Do something like this instead;
Expand|Select|Wrap|Line Numbers
  1. int subPosition(String s, String p) {
  2.  
  3.    int sn= s.length();
  4.    int pn= p.length();
  5.  
  6.    for (int i= 0; i <= sn-pn; i++) {
  7.       if (s.substring(i, i+pn).equals(p))
  8.          return i;
  9.       }
  10.    return -1;
  11. }
Jun 2 '07 #7
PvtBillPilgrim
19 New Member
OK. Sorry about this.
Everything works now, except for when the substring does not exist within the string and the string has more characters than the substring (i.e. the following loop).

.............. (I again omitted the if and else if at the beginning because I'm sure they work)
else
{
for (int i = 0; i < lengthStr; i++)
{
String mutation;
mutation = str.substring (i, i + lengthSubstring);
if (mutation.equals(substring))
{
position = i;
return position;
}
}
position = -1;
return position;
}
}

Again it compiles. But if I try something like "hamburger" for the larger string and "dog" for the substring, it gives me like ten red lines telling me my string index is out of range (10 with this instance).

Is there an easy way to solve this without jeopardizing the code that does work?
Thanks again (I'm just frustrated at this point.)
Jun 2 '07 #8
JosAH
11,448 Recognized Expert MVP
OK. Sorry about this.<snip>
Again it compiles. But if I try something like "hamburger" for the larger string and "dog" for the substring, it gives me like ten red lines telling me my string index is out of range (10 with this instance).

Is there an easy way to solve this without jeopardizing the code that does work?
Thanks again (I'm just frustrated at this point.)
No need to apologize; you're working on your problem and there's nothing wrong
with that. Think of it, suppose you have a String of five characters "abcde" and
suppose you have a pattern of two characers: "de". What would the maximum
value of i be from where you start your substring? Remember positions start at
zero. In order to rip out a substring of two characters you can rip out your
substring starting at position 5-2 == 3 at most. Further than that position you
can't build a substring of two characters.

This basically translates to: your for loop shouldn't loop over all possible values
less than the String's length. It should take the length of the pattern in account
too; reread my code snippet in my previous reply.

kind regards,

Jos
Jun 2 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
9856
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
0
1867
by: john | last post by:
Hi,All MS Tech Gurus: I need your help!!! It is the second time I post this message, I need get some feedback ASAP, Please Help!! Thanks a lot in advance. John I have a csharp method, using...
22
2142
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
5
3764
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
8
4975
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
8
2715
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
2
2881
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
1
2807
by: twin2003 | last post by:
need help with inventory part 5 here is what I have to do Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
1
2646
by: jcato77 | last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created. Assignment: Modify the Inventory Program by creating a subclass of the product class...
2
9995
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.