Jump to content

String class help

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
ok, so a few days ago I saw this thread: http://forum.codecal...ic-program.html
(for those who are too lazy for the link):

Quote

need logic for the program

I need logic for the following program. please help me

Question :
Accept 2 strings and check how many times the second string is in the first string.

Example1:

Enter main string : This is a dog which is angry. This is a dog which eat much.

Enter string to check: This is a dog
output
2 times the 2nd string is present in the main string.

and, being a java noob, I was encouraged to try to write this program to test my skills.

I have come this far and I think I'm pretty close, but I could be wrong.
import java.util.*;


public class Sentence {

	public static void main(String[] args){

		// This program is supposed to let the user input two strings and check to see how many times

		// the second string is in the first.

		String main, check;

		int i = 0, index = 0;

	

		main = "This is a dog which is angry. This is a dog which eat much.";

		check = "This is a dog";

		

		for(int length = 0; length < main.length(); length++){

			index = main.indexOf(check, index + 1);

			if(index >= 0){

			i += 1;

			}

		}

		System.out.println("The check string appears in main " + i + " times.");

	}

}


I thought I had coded it right, but obviously I didn't, because the output returns "The check string appears in main 39 times.
"

I'm 99% sure it has something to do with my for loop, can anyone help nudge me in the right direction? thanks :)

p.s. once I'm done with this program and it runs well, I'm going to change the value of the two string variables to scanner variables, that way the user can input whatever they like. I just wanted to test certain strings first.

Edited by Cruel Hand, 09 December 2011 - 05:49 PM.
p.s.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
if(index >= 0){

	i += 1;

}
You also need to check if the index is not the same as the previous time.

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Are you aware that this is the output of your program?
The check string appears in main 39 times.

Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
romps

romps

    Newbie

  • Members
  • Pip
  • 1 posts
Your problem is following
for(int length = 0; length < main.length(); length++){

			[B]index = main.indexOf(check, index + 1);[/B]

			if(index >= 0){

			i += 1;

			}

		}
Because initally index = 0 and when you first time call main.indexOf(check, index + 1) then index + 1 = 1, and you miss the first "This is a dog". But you find
next one, and it's index is 30. When you second time call main.indexOf, then you are doin'ng such call where main.indexOf(check, 30 + 1), and you get result -1, which shows that string wasn't found. Third time when you call main.indexOf, then your index variable is -1, you add 1 and get 0 and call is following main.indexOf(check, -1 + 1) and so on and on..... And you do'it for entire for cycle.


My solution is following.
import java.lang.Character.Subset;

import java.util.*;


public class Sentence {

	public static void main(String[] args){

		// This program is supposed to let the user input two strings and check to see how many times

		// the second string is in the first.

		String main, check;

		int i = 0, index = 0;

	

		main = "This is a dog which is angry. This is a dog which eat much.";

		check = "This is a dog";

		

		int mLen = main.length();

		int cLen = check.length();

		

		while ((index + cLen) < mLen) {

			if (main.substring(index, index + cLen).equals(check)) {

				i++;

			}

			index++;

		}

		

		System.out.println("The check string appears in main " + i + " times.");

	}

}

Sry for my bad english.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
By the way, the shortest I can come up with for your problem in general (counting occurences) is:
String main = "This is a dog which is angry. This is a dog";
String check = "This is a dog";

int occurences = (main+ " ").split(check).length-1;
System.out.println("Occurences: " + );



#6
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
I appreciate the help everyone, thanks!

fread said:

Are you aware that this is the output of your program?
The check string appears in main 39 times.
just curious, did you even read my thread?

to answer your question, yes, I am very aware that that is the output of my program, thus the reason I posted this thread.

Cruel Hand said:

I thought I had coded it right, but obviously I didn't, because the output returns "The check string appears in main 39 times.
"


#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Quote

just curious, did you even read my thread?
Yes I did. Well, skimmed. Sorry.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#8
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET

fread said:

Yes I did. Well, skimmed. Sorry.

no worries :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users