7 Replies - 1501 Views - Last Post: 01 November 2009 - 01:07 PM Rate Topic: -----

#1 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 71
  • Joined: 12-June 09

String extraction

Posted 01 November 2009 - 02:53 AM

How can I use substring to to tell how many times the word "code" appears? ("d" can be substituted for any letter.)

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2
Is This A Good Question/Topic? 0
  • +

Replies To: String extraction

#2 serafettin  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 30
  • Joined: 21-June 09

Re: String extraction

Posted 01 November 2009 - 03:02 AM

View PostAmateurC, on 1 Nov, 2009 - 01:53 AM, said:

How can I use substring to to tell how many times the word "code" appears? ("d" can be substituted for any letter.)

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2


well you can use a for loop for that.
int countcode = 0;
		String string = keyboard.next();
		for(int i=0; i <= string.length()-4; i++)
		{
			if(string.substring(i,i+3) == "code")
				countcode++;
		}


This post has been edited by serafettin: 01 November 2009 - 03:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,645
  • Joined: 20-September 08

Re: String extraction

Posted 01 November 2009 - 03:04 AM

Use String.indexOf in a while loop
Was This Post Helpful? 1
  • +
  • -

#4 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: String extraction

Posted 01 November 2009 - 06:51 AM

first you have an error:
if(string.substring(i,i+3)=="code")

will never work as Strings cannot be directly compred.

use:
if(string.substring(i,i+3).equalsIgnoreCase("code"))
{
//your work
}
also the substring goes till endinglimit -1 so you must have

if(string.substring(i,i+4).equalsIgnoreCase("code"))
{
//your work
countcode++;
}


This post has been edited by sakshamkum: 01 November 2009 - 06:52 AM

Was This Post Helpful? 1
  • +
  • -

#5 serafettin  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 30
  • Joined: 21-June 09

Re: String extraction

Posted 01 November 2009 - 07:46 AM

View Postsakshamkum, on 1 Nov, 2009 - 05:51 AM, said:

first you have an error:
if(string.substring(i,i+3)=="code")

will never work as Strings cannot be directly compred.

use:
if(string.substring(i,i+3).equalsIgnoreCase("code"))
{
//your work
}
also the substring goes till endinglimit -1 so you must have

if(string.substring(i,i+4).equalsIgnoreCase("code"))
{
//your work
countcode++;
}



Yes sorry about that. I am a begginer in java to and I did not try my code I just write it. Thanks again for your correction
Was This Post Helpful? 1
  • +
  • -

#6 cdog5000  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 79
  • Joined: 31-October 09

Re: String extraction

Posted 01 November 2009 - 07:48 AM

public static int count(String input, String search){
		int count = 0;
		for(int i = 0; i < input.length() - search.length(); i ++){
			if(input.substring(i, search.length()).equals(search)){
				count ++;
			}
		}
		return count;
	}

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,645
  • Joined: 20-September 08

Re: String extraction

Posted 01 November 2009 - 08:27 AM

You can avoid all the overhead of substringing if all you want to do is get a count. Use indexOf
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: String extraction

Posted 01 November 2009 - 01:07 PM

View Postserafettin, on 1 Nov, 2009 - 02:02 AM, said:

View PostAmateurC, on 1 Nov, 2009 - 01:53 AM, said:

How can I use substring to to tell how many times the word "code" appears? ("d" can be substituted for any letter.)

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2


well you can use a for loop for that.
int countcode = 0;
		String string = keyboard.next();
		for(int i=0; i <= string.length()-4; i++)
		{
			if(string.substring(i,i+3) == "code")
				countcode++;
		}



if(string.substring(i,i+3) == "code")
should be
if(string.substring(i,i+3).equals("code"))

or it will never work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1