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:
And the Main Class...
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
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