字节组合发生器和定序器

时间:2015-08-27 03:24:22

标签: java regex recursion binary byte

首先,这个问题将是关于相同代码打包成一个的2个问题。另外,请原谅我对语言的无知,我在过去两周才学习Java,这是我第一个重大项目的一部分。

请考虑以下代码:

public class TESTCODE {

public static ArrayList<String> bytePossibalitiyGenerator(int bits, String current) throws Exception { ArrayList<String> binaries = new ArrayList<>(); if (bits%8 != 0) { int crash = bits%8; throw new Exception("The bit count that you have entered is not divisable by 8:" + "\n" + "There is a remainder of: " + Integer.toString(crash)); } else { if (current.length() == bits) { binaries.add(current); return binaries; } // pad a 0 and 1 in front of current; binaries.addAll(bytePossibalitiyGenerator(bits, "0" + current)); binaries.addAll(bytePossibalitiyGenerator(bits, "1" + current)); System.out.println(binaries.toString()); } return binaries; } //The method below is supposed to format out the whitespace between binary strings and arrange the //data in such a way that each possible outcome is on a new line. //TODO Add a parser for the size of the byte ex. if the binary string is comprised of all of the possible //TODO outcomes for 1 byte than every 8 instead of appending a space, append a new line. public static String binarySequencer(String input) { StringBuffer toReturn = new StringBuffer(); //This StringBuilder is a safety precaution to ensure that if the algorithm is to be //run again, the value of each previously read and appended string position is nullified //so that it is not re-appended to the StringBuffer StringBuilder inputSB = new StringBuilder(input); //Setting the booleans for which type of string the input was (The raw binary array itself, or the array converted into a string) boolean rawBinaryArrayOutput = Pattern.compile("^[0-1,\\s]+$").matcher(input).find(); boolean stringBinary = Pattern.compile("^[0-1\\t]+$").matcher(input).find(); //This boolean is to check whether the loop has previously passed over 1 byte for sequencing boolean hasPassedAByte = false; //Safety if statements, because who doesn't love Java Exceptions and stack traces... if (rawBinaryArrayOutput == false && stringBinary == false || rawBinaryArrayOutput == true && stringBinary == true) { System.out.println(rawBinaryArrayOutput); System.out.println(stringBinary); //TODO Find a way to print the stack trace... throw new InputMismatchException(); } else { int runLength = 0; for (int i = 0; i < inputSB.length(); i++) { int j = 0; if (stringBinary == true && rawBinaryArrayOutput == false) { if (runLength == 8 && (inputSB.charAt(j += i) == 0 || inputSB.charAt(j += 1) == 1)) { toReturn.append(Character.toString(' ')); runLength = 0; hasPassedAByte = true; } else { if (hasPassedAByte = true && runLength == 8 && (inputSB.charAt(j) != 0 || inputSB.charAt(j) != 1)) { toReturn.append("\n"); runLength = 0; hasPassedAByte = false; } while (i + 1 < inputSB.length() && (inputSB.charAt(i) == 0 || inputSB.charAt(i) == 1) && runLength != 8) { runLength++; toReturn.append(inputSB.charAt(i)); inputSB.insert(i, null); i++; } } } else { if (rawBinaryArrayOutput == true && stringBinary == false) { //Insert code for formatting the raw binary array output System.out.println("You haven't added this code yet :p"); } } } } return toReturn.toString(); } public static void main(String[] args) throws Exception { String toBeSequenced = ""; for (String s : bytePossibalitiyGenerator(16, "")) { toBeSequenced += s + "\t"; } System.out.println(binarySequencer(toBeSequenced));}}

现在提出问题:

1:对于boolean rawBinaryArrayOutput我使用java.util.regex.Pattern类'compile方法搜索正在输入的字符串中的{0-1 , \\s}字符,如果找到任何字符,它将rawBinaryArrayOutput设置为true。如果找到所有这些值中的至少一个,有没有办法让它只将rawBinaryArrayOutput设置为true?

2:在binarySequencer方法中,我StringBuilder inputSB会自动获取String input的值,以便我可以修改StringBuilder中的值。在第75行,我试图将位置i的值设置为null,这样如果while某种方式循环在同一位置上运行两次,它就不会向StringBuilder toReturn添加任何内容,但Eclipse在该行The method (int, Object) is ambiguous for type StringBuilder上给出了编译错误。这是什么意思,我该如何解决?

3:我在binarySequencer方法的开头有一个if语句,用于检查rawBinaryArrayOutput == false && stringBinary == false || rawBinaryArrayOutput == true && stringBinary == true是否为throw new InputMismatchException();,如果它们是myObj = data。如果可能的话,我怎样才能打印出堆栈跟踪?

0 个答案:

没有答案