OK, you'll need to create two separate java files out of the code and execute the Main Class, but it's a really neat program. Took me a bit to figure out how to make it all work until I remembered ArrayLists. Now it works like a charm!
Anyway, here is the code for those of you that want to check it out and admire my handiwork.
Main CLASS
myString CLASS
Anyway, here is the code for those of you that want to check it out and admire my handiwork.
Main CLASS
Code:
public class Main { public static void main (String args[]) { myString ms = new myString("sdroxebra","boxer"); System.out.println(ms.getMix()); System.out.println(ms.getInput()); System.out.println(); System.out.println(ms.checkString()); //SHOULD WORK! Must return true and the string "boxer" System.out.println(); myString m1 = new myString("rogdraodmpe","programme"); System.out.println(m1.getMix()); System.out.println(m1.getInput()); System.out.println(); System.out.println(m1.checkString()); //SHOULD NOT WORK! Must return false and the string "programe" //11:25am - Not working, returning true //11:48am - Working perfectly, used ArrayList<Character> System.out.println(); myString m2 = new myString("rogdraodmpe","dog"); System.out.println(m2.getMix()); System.out.println(m2.getInput()); System.out.println(); System.out.println(m2.checkString()); //SHOULD WORK! Must return true and the string "dog" //11:21am - not working because the method saves two Ds into the temp //11:24am - working now with break command }//end method main }//end class Main
Code:
import java.util.*; public class myString { private String mix = ""; private String input = ""; public myString(String mix,String input) { this.mix = mix; this.input = input; }//end constructor public String getMix() { return mix; }//end method getMix . returns the variable mix saved by constructor bzw. user public String getInput() { return input; }//end method getInput . returns the variable input saved by constructor bzw. user public boolean checkString() { String temp = ""; ArrayList<Character> inputArray = new ArrayList(); ArrayList<Character> mixArray = new ArrayList(); for (int i = 0 ; i < input.length() ; i++) { inputArray.add(input.charAt(i)); }//converts input into an ArrayList of Characters to be compared against mix for (int i = 0 ; i < mix.length() ; i++) { mixArray.add(mix.charAt(i)); }//converts mix into an ArrayList of Characters to be compared against input for (int current1 = 0 ; current1 < inputArray.size() ; current1++) { if (mixArray.size() == 0) break; //If mixArray is empty after all the deletions, loop ends for (int current2 = 0 ; current2 < mixArray.size() ; current2++) { if(mixArray.get(current2) == inputArray.get(current1)) { temp += inputArray.get(current1); mixArray.remove(current2); //add char value to temp string, remove matched char from mix if(temp.equals(input)) { System.out.println(temp); return true; }//end method when temp matches input break; //break loop and move to next character in input } } } System.out.println(temp); return false; //If temp was never matched to input, return false and print out unmatched temp }//end method checkString2 }//end class myString
Last edited by CapnNismo (2009-12-10 03:02:53)