Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2008, 11:28 PM
Member
 
Join Date: Feb 2008
Posts: 5
Nuluvius is on a distinguished road
Need a little help with a function!
Hi,

I'v got a slight problem, Im sure its quite simple to solve - I just dont know how to order the syntax :/

I have to solve the following problem:

Design, code and test a function:

public static boolean contains(String str1,String str2)
// pre: str1 and str2 not null
// post: returns true if str2 is a substring of str1.

Hint: you will need a loop, and will need to know the length of str1 and str2.

For the solution I have so far worked out that I need to make use of the Pattern and Matcher methods to determin if str2 (in its entirity) exists anywhere within str1. Any assistance would be greatly apprechiated!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-07-2008, 11:44 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Easy solution
Welcome, Nuluvius, to Java Forums

There is a very easy solution for your problem. You can use the String.indexOf() method to determine if a String is a substring of another. I did this:
Code:
public static boolean contains(String string1, String string2){ return -1 != string1.indexOf(string2); }
In my testing class, I had:
Code:
package pack; import java.util.*; public class Test{ public Test(){ System.out.println("Test running"); System.out.println("Enter string1:"); String string1 = Tools.readln(); System.out.println("Enter string2:"); String string2 = Tools.readln(); System.out.println("Result = " + contains(string1, string2)); } public static boolean contains(String string1, String string2){ return -1 != string1.indexOf(string2); } }
output:
Code:
Test running Enter string1: true love Enter string2: love Result = true
I hope this helps you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2008, 12:07 AM
Member
 
Join Date: Feb 2008
Posts: 5
Nuluvius is on a distinguished road
Firstly, thank you for the welcome!

Secondly, that is amazing, I had actualy spent about 2/3 hours on this particular problem.. yet it was so simple. Thank you very much for sloving it and putting me out of my angush.

I think it goes without saying that I am only just learning java lol. This is my first year of a Software Engineering degree. I shal definatly be returning with my future conundrems
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-08-2008, 12:33 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Glad to help
I'm glad to help Nuluvius. I am currently studying a B.Sc. in Computer Science and I believe that your instructor either wants you to formulate an algorithm for this problem or use regular expressions. Anyway, good luck!
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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
use of onclick function m4tt New To Java 1 02-16-2008 04:03 AM
How to use a function in java olikhvar New To Java 2 02-16-2008 03:57 AM
I want to add function romina New To Java 1 08-07-2007 06:25 AM
function name osval Advanced Java 1 08-06-2007 09:56 PM
Help with the Dummy Function Felissa JavaServer Pages (JSP) and JSTL 1 07-06-2007 06:03 PM


All times are GMT +3. The time now is 11:54 AM.


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