String problem!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: String problem!

  1. #1
    Join Date
    Oct 2003
    Posts
    4

    String problem!

    Hi everybody ,
    i got a problem with my Program.

    this is the Programm:
    Code:
    public class check
      {
         public static void main(String[] args)
         {
            String text = "Loooppa";
    
            int oCount = 0;
            int index = -1;
            int i=0;
            String oStr="o";
            int textLength = text.length();
    
            index = text.indexOf(oStr);
    
            while(index>=0)
            {
    
              index = index + oStr.length();
              index = text.indexOf(oStr,index);
              oCount++;
            }
                             
           System.out.println(+oCount +oStr);
      }
    }

    After i run the program i become:
    3o
    -Now my question is how can i become this output:
    L3oppa
    thx in advance

    [ArchAngel added CODE and QUOTE tags]

  2. #2
    Join Date
    Oct 2003
    Posts
    9
    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.

    Ralpharama.

  3. #3
    Join Date
    Oct 2003
    Posts
    4

    hi ralpharama

    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

  4. #4
    Join Date
    Mar 2003
    Posts
    834

    Arrow

    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:

    In: "Loooppa"
    Out: "L3oppa"

    (personally, I would have thought you'd have wanted the output to be "L3o2pa")
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Oct 2003
    Posts
    4

    hi ArchAngel

    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:

    in:Loopppa
    out:L2o3pa

    in:asssdddddd
    out:a3s6d

    The idea from ralpharama is good but to hard for me.

    thx for the replay

  6. #6
    Join Date
    Oct 2003
    Posts
    9
    Something like this - sloppy programming, but it may give you some ideas?
    Code:
    {
    String text = "Loooppa";
    String oStr="o";
    text=replacerizer(text,oStr);
    String oStr="p";
    text=replacerizer(text,oStr);
    etc...
    }
    
    
    public String replacerizer(string text, string oStr)
    {
    // Replace all of the search string with '*'s
    text.replaceAll(oStr,"*");
    
    // Init our counter and new string
    int starCount=0;
    String newText="";
    
    // Loop through string, count *s and create new string
    for (int i=0; i<text.length(); i++) 
    {
     // s is a one char string
     String s=text.subString(i,i);
    
     // if s is a *, then increment starCounter
     if (s.indexOf("*"))>-1 
     {
    	starcount++;
     }
     // Otherwise...
     // if starCounter is greater than 0 then add the number to the newText string
     // then, add the current char to the newText string too...
     else 
     {
    	if (starCount>0) newText+=""+starCount+oStr;
    	starCount=0;
    	newText+=s;
     }
    }
    return newText;
    }
    P.S. Haven't tested this, it may work, it may not!

    [ArchAngel added CODE tags]

  7. #7
    Join Date
    Mar 2003
    Posts
    834
    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:

    Code:
    Keep track of the current character: "current"
    Keep track of the number of times the current character has appeared in a row: "counter"
    REPEAT
      Move to the next character
      IF this character is the same as the previous THEN  
        Increment "counter"
      ELSE
        IF the "counter" is greater than 0 THEN
          display "counter" followed by "current"
        ELSE
          display "current"
        END IF
        Update "current" to be the new character
        Reset "counter" to zero.
      END IF
    END REPEAT
    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.
    ArchAngel.
    O:-)

  8. #8
    Join Date
    Oct 2003
    Posts
    4

    :(

    hello ArchAngel, this is how i interpret your code:
    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
    
         oCount++;//Increment "counter"
        
        if(oCount>0) //IF the "counter" is greater than 0 THEN
         
         System.out.print(+oCount +oStr); //display "counter" followed by "current"
    
        
        else
         
         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]

  9. #9
    Join Date
    Mar 2003
    Posts
    834

    Thumbs down

    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:

    Prev.............Curr
    ''..................'H'
    'H'..................'e'
    'e'..................'e'
    'e'..................'l'
    'l'..................'l'
    'l'..................'o'

    (the dots are just to preserve the spacing in this post)

    Here's the skeleton code:
    Code:
    public class Test {
            public static void main(String[] args) {
                    convert("Heello");
            }
    
            public static void convert(String originalString) {
                    // Keep track of the index of the "previous character".
                    int previousIndex = -1;
    
                    System.out.println("Prev \t\t Curr");
    
                    // FOR each character in "originalString":
                            // Display "previous character" (output '' 
                            // Display "current character".
    
    
                            // Updated "previous character" index.
                    // END FOR
            }
    }
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center