public class check
public static void main(String args)
String text = "Loooppa";
int oCount = 0;
int index = -1;
int textLength = text.length();
index = text.indexOf(oStr);
index = index + oStr.length();
index = text.indexOf(oStr,index);
After i run the program i become:
-Now my question is how can i become this output:
thx in advance
[ArchAngel added CODE and QUOTE tags]
10-08-2003, 07:39 PM
What would happen if your string was 'joookool'? Would you want 'j3k2l'?
If you want to use multiple strings to search and count, like 'ez' in 'jezezezkezl' giving 'j3k2l', then I'd perhaps:
Replace all the strings (say 'ez') with a single char (say *), [using String.replaceAll?]
Then create a temporary string,
Then create a simpler loop to step through the new string (containg the *s), with an 'IF' saying
'if it's not a *, add it to the new tempstring, but if it is a * then count them until there are no more *s, then add the number to the temp string and move on.'
Then output the temp string.
Well it would work.
10-09-2003, 05:47 AM
hi ralpharama and thx for posting on my question...
ur idea is great,but to hard for a beginner like me.I have a question,what u mean with :
....single char (say *).. how can i declare the symbol: *
do u mean:
char sym = "*"; ???
i tried but i can't do it right.
Anyway do u think the book:
Beginning Java 2 from Ivor Horton
is the right one for beginners like me???
thx for ur replay
10-09-2003, 08:29 AM
I do - it's a brilliant book.
I'm a bit confused as to exactly what you want this program to do. Please give a few input/output examples:
(personally, I would have thought you'd have wanted the output to be "L3o2pa")
10-09-2003, 01:31 PM
well the output: L3o2pa is correctly what i want but as i'm a beginner i dunno how to do it.Should i use twise "while" command???And the most importent is how can i do this for every STRING.Lets say i use my keyboard and my code should do the following:
The idea from ralpharama is good but to hard for me.
thx for the replay
10-09-2003, 04:04 PM
Something like this - sloppy programming, but it may give you some ideas?
public String replacerizer(string text, string oStr)
// Replace all of the search string with '*'s
// Init our counter and new string
// Loop through string, count *s and create new string
for (int i=0; i<text.length(); i++)
// s is a one char string
// if s is a *, then increment starCounter
// if starCounter is greater than 0 then add the number to the newText string
// then, add the current char to the newText string too...
if (starCount>0) newText+=""+starCount+oStr;
P.S. Haven't tested this, it may work, it may not!
[ArchAngel added CODE tags]
10-09-2003, 05:48 PM
OK. You just have to think about this logically and be able to work out the steps required ON PAPER before you start coding. Something like:
Keep track of the current character: "current"
Keep track of the number of times the current character has appeared in a row: "counter"
Move to the next character
IF this character is the same as the previous THEN
IF the "counter" is greater than 0 THEN
display "counter" followed by "current"
Update "current" to be the new character
Reset "counter" to zero.
I've had a run through this a couple of times and it seems about right...understand it and then have a go at coding it yourself.
10-10-2003, 04:10 PM
hello ArchAngel, this is how i interpret your code:
index = text.indexOf(oStr); //Keep track of the current character: "current"
while(index>=0) //Keep track of the number of times the current character has appeared in a row: "counter"
index = index + oStr.length();//Move to the next character
if(oCount>0) //IF the "counter" is greater than 0 THEN
System.out.print(+oCount +oStr); //display "counter" followed by "current"
System.out.print( oStr); //display "current"
index = text.indexOf(oStr,index); //Update "current" to be the new character
oCount=0; //Reset "counter" to zero.
the output is: 1o1o1o
a question:how can i track if the charackter is same as the previos,i cant convert string into boolean(that is saying my compiler)...
[CODE tags added by ArchAngel]
10-18-2003, 01:38 PM
You don't seem to have got the idea - it looks like you just tried to do a straight translation of my pseudocode.
Let's try doing incremental development. Start off with something simple. Write a program which when given the String "Heello", will output: