Benzin
Member
+576|6244
OK, I've got an algorithm issue here that I can't work out. I know where and why the problem is coming up (it's when I delete an element out of the ArrayList) - whenever I have two numbers standing next to each other in the String that I pass to the constructor, it'll only delete the first one and skip the second, which is really annoying.

Anyway, here's the code:

Code:

import java.util.*;

public class Compress
{
    private String internal = "";
    private Integer i = 0;
    
    public Compress(String internal)
    {
        this.internal = internal;
    }//end constructor Compress
    
    public Integer compress()
    {
        ArrayList<Character> c = new ArrayList();
        
        int tempint = 0;
        String temp = "";
        
        for (int i = 0 ; i < internal.length() ; i++)
        {
            c.add(internal.charAt(i));
        }//end for, add all elements of internal to ArrayList c
        
        for(int i = 0 ; i < c.size() ; i++)
        {
            tempint = c.get(i) - '0';
            //convert char to an int value
            
            if(tempint >= 0 && tempint <= 9)
            {
                temp += c.remove(i);
            }//end if, check if char is an integer
            
        }//end for
        
        Integer intObj = new Integer(temp);
        //Initialize Integer Class i with String variable temp
        
        this.clearInternal();
        //Clears internal of any characters, so that way only letters
        //can be added
        
        for(int i = 0 ; i < c.size() ; i++)
        {
            internal += c.get(i);
        }//Add all chars out of the ArrayList back to internal
        
        return intObj;
    }//end method compress
    
    public String getInternal()
    {    
        return internal;
        //send internal's value back to the calling program
    }
    
    private void clearInternal()
    {
        internal = "";
        //delete the internal variable of any data
    }
}//end class Compress
And the Main Class...

Code:

public class Main
{
public static void main (String args[])
{
    Compress c = new Compress("1Dies 2ist 3ein 5Programmier00test2");
    
    System.out.println(c.getInternal());
    System.out.println(c.compress());
    System.out.println(c.getInternal());
    System.out.println();
    
    Compress c1 = new Compress("1Di2es 2i7st 3ein 5Prog43rammier0test2");
    
    System.out.println(c1.getInternal());
    System.out.println(c1.compress());
    System.out.println(c1.getInternal());
    
}//end method main
}//end class Main
-TL-
Srs lurker
+25|6738|Oklahoma City
I belive your problem is with

Code:

if(tempint >= 0 && tempint <= 9)
{
     temp += c.remove(i);
}
Each time you remove a character from the ArrayList, all characters after it are shifted one index to the left, but your loop continues from the index to the right of the one removed, so you skip one character for every one you remove.

To fix it, try

Code:

if(tempint >= 0 && tempint <= 9)
{
      temp += c.remove(i);
      i--;
}

Last edited by -TL- (2009-12-10 20:12:44)

GravyDan
Back from the Grave(y)
+768|6178|CA

-TL- wrote:

I belive your problem is with

Code:

if(tempint >= 0 && tempint <= 9)
{
     temp += c.remove(i);
}
Each time you remove a character from the ArrayList, all characters after it are shifted one index to the left, but your loop continues from the index to the right of the one removed, so you skip one character for every one you remove.

To fix it, try

Code:

if(tempint >= 0 && tempint <= 9)
{
      temp += c.remove(i);
      i--;
}
I was just about to say that, only better.
Benzin
Member
+576|6244
Works like a charm, thanks!

Board footer

Privacy Policy - © 2024 Jeff Minard