字节组合发生器和序列器



首先,这个问题将是关于相同代码的两个问题。另外,请原谅我对这门语言的无知,我在过去的两周才学会Java,这是我第一个主要项目的一部分。

考虑以下代码:

<>之前公共类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为真,如果发现所有这些值中的至少一个?

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();。如果可能的话,我该如何让它也打印出堆栈跟踪呢?

  1. 对于rawBinaryArrayOutput,您可以尝试使用regex:

    ^([01](,s)?)+$
    

    将接受具有01序列的字符串(一个或多个char)由可选的,s部分分隔。但是对于比特输入,可能最好是:

    ^(?:(?:[01]{8})+(?:,s)?)+$
    

    只接受0或8个字符序列中的一个或多个1

  2. ?:部分用于不按regex分组。
  3. The method (int, Object) is ambiguous for type StringBuilder据我所知,意味着StringBuilder类有两个类似的方法,在这种情况下,它是:insert(int, String)insert(int, char[]),以及编译器没有知道要调用哪个变量,因为作为第二个变量使用null, null引用可以转换为任何类类型的表达式。试一试:

    inputSB.insert(i, (char[]) null); 
    
    例如

    。那么你调用哪个方法就很明显了,即使它是

  4. 我不会使用InputMismatchException(),最好抛出另一个自定义异常。使用正确的正则表达式,将没有双真匹配的选项,而使用双假匹配,您可以只打印有用的信息。

最新更新