Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 12-05-2007, 05:02 PM
Member
 
Join Date: Dec 2007
Posts: 7
definewebsites is on a distinguished road
tricky indexOf implementation -- Help!!
Hi there,

I am implementing the indexOf method of the string class to check whether a string value exists in another as in the code below:

Code:
String str = "9,10,11,12"; String isIn = JOptionPane.showInputDialog("Please enter a number"); int x = str.indexOf(isIn); if(x != -1) { System.out.println("Not in"); } else { System.out.println("in"); }

The problem here is that if the user enters "1", 1 is in the string as the number 11. How would I be able to write the code such that it can differentiate between 1 and 11? I have thought about it and I just cannot figure out how it can be done. I am new to java and I would appreciate some assistance in this regard.

Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-05-2007, 05:30 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
You could just check the character right after the 1 that you find and make sure it isn't a 1 as well.

Code:
int i = String.indexOf("1"); if( i != -1 && !String.charAt(i).equals("1")){ }
You could also do an indexOf("1") and an indexOf("11") and compare them. If they are the same then you have 11 and not 1.

The problem with these solutions is that they are very specific. I would probably write my own method using the first technique. I would pass it the character to look for and it would return the position only if the character was repeated.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-06-2007, 12:28 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
String str = "9,10,11,12"; String isIn = " "; do { isIn = JOptionPane.showInputDialog("Please enter a number"); if(isIn == null) break; int x = str.indexOf(isIn); boolean confirmed = false; if(x != -1) { boolean hasLeadingComma = x > 0 && str.charAt(x-1) == ','; boolean hasTrailingComma = x < str.lastIndexOf(",") && str.charAt(x + isIn.length()) == ','; boolean isFirst = x == 0 && hasTrailingComma; boolean isLast = x > str.lastIndexOf(",") && hasLeadingComma; confirmed = isFirst || isLast || (hasLeadingComma && hasTrailingComma); } if(confirmed) { System.out.println("Found " + isIn + " at index " + x); } else { System.out.println(isIn + " not found, x = " + x); } } while(isIn.length() > 0);
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-10-2007, 02:48 AM
Member
 
Join Date: Dec 2007
Posts: 7
definewebsites is on a distinguished road
Re:
Thanks alot for your assistance, I do appreciate all the help that has been offered..

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
Custom ArraySet implementation Java Tip java.lang 0 04-14-2008 10:41 PM
VietPad 2.0 (.NET implementation) Java Tip Java Announcements 0 04-05-2008 06:32 PM
Graph DPS and BFS implementation hey New To Java 1 01-09-2008 11:19 PM
Help with recursive implementation toby Advanced Java 1 08-07-2007 07:57 AM
Help regarding indexOf gauravj New To Java 1 07-10-2007 03:12 PM


All times are GMT +3. The time now is 04:33 AM.


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