Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2009, 11:41 AM
Member
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0
The Hawk is on a distinguished road
Default Searching the first occurence
Hi everybody,

i should write a methode, which gives the first occurence of a substring in a string with an index.

If the string starts with the substring so the index must be 0, if the substring does not exist in the string so the index is -1. If the substring.length is 0 so the index is 0.

The method is:

Code:
public static int getFirstOccurence(char[] str, char[] substr){
                  //TO DO
}
The strings are changed into the array. In this method we are working with arrays which have charakters. (I've already wrote the methode to change strings into the array...)

Can anybody help me? I don't know how to search an array, in an other array?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-27-2009, 02:06 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,364
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by The Hawk View Post
Hi everybody,

i should write a methode, which gives the first occurence of a substring in a string with an index.

If the string starts with the substring so the index must be 0, if the substring does not exist in the string so the index is -1. If the substring.length is 0 so the index is 0.

The method is:

Code:
public static int getFirstOccurence(char[] str, char[] substr){
                  //TO DO
}
The strings are changed into the array. In this method we are working with arrays which have charakters. (I've already wrote the methode to change strings into the array...)

Can anybody help me? I don't know how to search an array, in an other array?
Better leave those Strings as Strings and use the indexOf() method (read the API documentation for the String class).

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-28-2009, 10:07 PM
Member
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0
The Hawk is on a distinguished road
Default
Thank you for your answer,

but this is an homework and i should do it with arrays. Is there any other ideas, or some tipps?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-28-2009, 10:47 PM
Senior Member
 
Join Date: Nov 2009
Posts: 139
Rep Power: 0
dinosoep is on a distinguished road
Default
So we are solving your homework?
please, learn something...
but anyway make a for loop for al the charachter
check if the character is the first character of the searched string
if so, check if the next charachter of the string is the next character of the searched string
(another for loop)
hope this helps and that you solve youre homework by yourself
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-28-2009, 10:54 PM
Member
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0
The Hawk is on a distinguished road
Default
thanks dinosep,

i don't want you to solve my homework, it's only a little part of whole homework. I couln't do it. I needed some help. Every tipp can be really helpfull.

i'll try your tip. thanks again
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-29-2009, 10:24 AM
Senior Member
 
Join Date: Nov 2009
Posts: 139
Rep Power: 0
dinosoep is on a distinguished road
Default
ok,tell me if it works.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-29-2009, 12:13 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,364
Rep Power: 5
JosAH is on a distinguished road
Default
The OP can smuggle a bit: look in the directory where the JDK is stored; it contains a file src.zip; it contains the source code for all the publicly avaialable classes. The source for the String class is also in there and that class contains the indexOf( ... ) method. Don't tell them I told you ;-)

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-29-2009, 12:36 PM
Senior Member
 
Join Date: Nov 2009
Posts: 139
Rep Power: 0
dinosoep is on a distinguished road
Default
thanks jos, what you told there was something i didn't know and surely are going to use
rep+
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching XML file using DOM simon XML 5 03-30-2009 01:27 AM
count occurence of word in a line of text sinyi88 New To Java 19 02-28-2009 07:37 AM
Problem with searching while using GUI BHCluster AWT / Swing 1 04-24-2008 06:56 PM
Searching an arraylist adelgado0723 New To Java 1 04-15-2008 01:09 PM
searching nalinda New To Java 3 12-06-2007 02:56 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:06 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org